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]))
Related
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=" ")
I want to write a program that prints a word 3 times. The first two iterations should have a space after them, but the 3rd line shouldn't.
Example of desired output: https://imgur.com/43pYOI9
This is the code I'm trying:
n = input("Enter a word: ")
for x in range(len(n)):
print((n[x]+" ")*3)
n.rstrip()
This is the output I'm getting - rstrip() doesn't seem to be working.
https://imgur.com/a/NM1uEQI
Any suggestions on how to fix this?
You need to rstrip() before you print and you need to rstrip() what you are actually printing, not the whole line. This would work
n = input("Enter a word: ")
for x in range(len(n)):
s = (n[x] + " ") * 3
print(s.rstrip())
A better way would be to use join()
n = input("Enter a word: ")
for x in range(len(n)):
print(" ".join(n[x] * 3))
And you can iterate over string directly, so this would also work
n = input("Enter a word: ")
for x in n:
print(" ".join(x * 3))
Or you could get clever and do something like this
n = input("Enter a word: ")
list(map(print, [" ".join(i * 3) for i in n]))
You almost had it. You're calling rstrip() after the print line which has no impact since it's already been printed.
n = input("Enter a word: ")
for x in range(len(n)):
print((n[x]+" ")*2 + n[x])
I have a program which transposes a matrix. My question is how to remove the space after every last number in row.
I know that the space is there because of the end = " ". The end makes a space after every number but how can I remove it from the last number in rows? How can I replace the end?
p = input ()
M = str(p).split(" ")
A = int(M[0])
R = int(M[1])
X=[]
for i in range(A):
l=list(map(int,input().split(" ")))
X.append(l)
for i in range(R):
for j in range(A):
print(X[j][i], end = " ")
print ()
It sounds like you only want spaces in between each number. The best way to do this is the join method, which operates on a delimiter, takes a list of strings, and creates a new string where the delimiter separates each string in the list.
For example,
>>> " ".join(["1", "2", "3"])
'1 2 3'
You can use this in your code. Here is the final version:
p = input()
M = str(p).split(" ")
A = int(M[0])
R = int(M[1])
X = []
for i in range(A):
l = list(map(int, input().split(" ")))
X.append(l)
for i in range(R):
row = [str(X[j][i]) for j in range(A)]
print(" ".join(row))
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='')
I'm trying to create a iso triangle (one that starts in the middle).
I have a code but the problem is that I'm not allowed to use Y* "*" 5 in my code.
(The y is a variable there)
Also I may only use one print statement at the end of my code.
Can you please help me out.
f = int(raw_input("enter"))
for i in range(f):
print " " * (f-i-1) + "*" * (2*i+1)
creats this triangle
*
***
*****
*******
*********
***********
However, you are not allowed to use the *-operator on string and int. So for example ''***'' * 3 is not allowed, but 3 * 4 is
This just creates a continuous string and then prints it at the end
f = int(raw_input("Enter height: "))
s = ''
for i in xrange(f):
for j in xrange(f-i-1):
s += ' '
for j in xrange(2*i+1):
s += '*'
s += '\n'
print s
This is a solution which i think is very easy to understand. You can make the parameter of range() variable, to make it more dynamic.
from __future__ import print_function
for i in range(1,12,2):
count=(11-i)/2
for j in xrange(count):
print(" ",end='')
for j in xrange(i):
print("*",end='')
for j in xrange(count):
print(" ",end='')
print(end="\n")
I think the best solution is using the center() string method:
f = int(raw_input("How many rows to print in the triangle? "))
star = "*"
full_string = ""
for X in xrange(f):
star += "**" if X>0 else ""
full_string += star.center(2*f-1) + "\n"
print full_string[:-1]
The str.center() documentation:
https://docs.python.org/2/library/string.html#string.center
EDIT: If you can't use the print statement within the for loop, you could concatenate the string during the loop and print it at the end:
f = int(raw_input("How many rows to print in the triangle? "))
star = "*"
full_string = ""
for X in xrange(f):
# the first row should take only one star
star += "**" if X>0 else ""
star2 = star.center(2*f-1)
full_string += star2 + "\n"
# slice the string to delete the last "\n"
print full_string[:-1]
I noticed that using a for loop add a newline character. If you want to avoid this, you can slice the string before printing.
There is no problem with this code, i just checked it and it worked fine. If you would post the error message we might be able to help a bit more.