Need to make rows and columns - python

nRow=int(input("No. of rows: "))
nCol=int(input("No. of columns: "))
ch=input("Which characters? ")
i=1
j=1
while j<=nCol:
while i<=nRow:
print(ch, end='')
i=i+1
print(ch, end='')
j=j+1
Here I tried my best to make a column and row in while loops, but it just didn't work out. When I input row and column numbers, they just gin one row. Like (Rows:2 Columns:3 characters:a), I expected to get like a table with 2 rows and 3 columns, but I got just - aaaaa.

You just need to change your loop so you go to new line after every row. You can also use for in range like this
for row in range(nRow):
for col in range(nCol):
print(ch, end=' ')
print()

Put rows in the outer loop and columns in the inner loop. At the end of each row reset the column counter and print a newline.
while j<=nRow:
while i<=nCol:
print(ch, end='')
i=i+1
i = 1
print()
j=j+1

Related

Python- Printing sequence of integers separated by commas

L=input().split(' ')
for i in L:
c=float(i)
print(int(c), sep=",")
A sequence of numbers separated by space is to be accepted from user, the numbers are converted to the greatest integer less than or equal to the number and then printed.
I wanted to print the output on same line, but my code doesn't work
Thanks in advance....
You are looping through the list, change to float and put it into variable c. That loop is essentially meaningless. After that loop c contains the last value in the iteration. Just print the result at once:
L=input().split(' ')
all_ints = map(int, L)
print(*all_ints, sep=',')
Or using str.join:
print(','.join(map(str, all_ints)))
Or using a loop:
for i in all_ints:
print(i, end=',')
To get them all as floats you can use map as well:
all_floats = list(map(float, L))
I think this should work for you:
L = input().split(' ')
for i in L:
c = float(i)
print(int(c), end=" ")

(Python 3) avoid printing comma at the end in print(variableName,end=',')

I have a code that print x number of numbers. Firstly, I asked for the serious length. Then print all the previous numbers (from 0 to x).
My question is that:
when printing these number, I want to separate between them using comma. I used print(a,end=',') but this print a comma at the end also. E.g. print like this 1,2,3,4,5, while the last comma should not be there.
I used if statement to overcome this issue but do not know if there is an easier way to do it.
n=int(input("enter the length "))
a=0
if n>0:
for x in range(n):
if x==n-1:
print(a,end='')
else:
print(a,end=',')
a=a+1
The most Pythonic way of doing this is to use list comprehension and join:
n = int(input("enter the length "))
if (n > 0):
print(','.join([str(x) for x in range(n)]))
Output:
0,1,2
Explanation:
','.join(...) joins whatever iterable is passed in using the string (in this case ','). If you want to have spaces between your numbers, you can use ', '.join(...).
[str(x) for x in range(n)] is a list comprehension. Basically, for every x in range(n), str(x) is added to the list. This essentially translates to:
data = []
for (x in range(n))
data.append(x)
A Pythonic way to do this is to collect the values in a list and then print them all at once.
n=int(input("enter the length "))
a=0
to_print = [] # The values to print
if n>0:
for x in range(n):
to_print.append(a)
a=a+1
print(*to_print, sep=',', end='')
The last line prints the items of to_print (expanded with *) seperated by ',' and not ending with a newline.
In this specific case, the code can be shortened to:
print(*range(int(input('enter the length '))), sep=',', end='')

Removing comma and quotations when printing list

When I am printing the list, there is a comma and quotations in the letter X, how do I remove it?
#asks user input
m = int(input("Enter number of boxes horizontally and vertically: "))
n = int(input("Enter number of mines: "))
a=[]
for i in range(m):
a.append([])
for k in range(m):
a[i].append("X")
i=1
#prints the generated cells
for i in range(m):
print a[i]
i=i+1
You are looking to use join to make your list in to a string. You want to make your string space separated, so you will want to use ' '.join():
Change this:
print a[i]
to this:
print(' '.join(a[i]))
Or, if you are mixing types, you should do:
' '.join(str(x) for x in a)
you can also use this:
print ' '.join(map(str, a[i]))

How to create a right triangle with asterisk without def function?

So far what I have is:
base = int(input("Enter a value"))
for row in range(base):
for colomb in range(row+1):
print('*', end='')
print()
You were nearly there. You just need to unindent the last print(). Example -
for row in range(base):
for colomb in range(row+1):
print('*', end='')
print()
Sharon's answer is the quickest solution to make the code you have work, but you could also do fewer runs through for loops by just printing (once) the entire string. "a" * 3 is "aaa", for instance, so you could do:
for row in range(1, base+1): # now the range runs [1-base] instead of [0-base-1]
print("*" * row)

Python - dictionary print the key as many times as his value

I want to print out all the letters in my dictionary (seen at the last line of my code) the problem is that the output is aqlmui now. But as you guys can see the l in my dictionary is having a value of 2 so I want to print that out 2 times. so the output of my program should be: aqllmui.
Help would be appreciated a lot! :)
def display_hand(hand):
row = ''
for letter in hand:
row += letter
#I think i need to put an if statement here but I just don't know how to do it
print row
display_hand({'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1})
You can also try print "".join(k*v for (k,v) in s.iteritems()).
Here k*v for (k,v) in s.iteritems() returns a list of key*value like ["a","i","m","ll","q","u"] and "".join(list) will join that list to make a string.
just do:
row += letter * hand[letter]
You can use string/int multiplication to perform multiple concatenations
for letter in hand:
row += letter * hand[letter]
Or a little more clearly and efficiently:
for letter, count in hand.iteritems(): # Use hand.items() in Python 3
row += letter * count

Categories

Resources