Issue
I am trying to print a string, multiple times together on the same line.
For example: User input = 123 and I need to print it 3 times: 123123123
This is my code that i have tried:
userString = []
if val > 0:
for i in range(val):
print(userString * val, end = " ")
it's giving me a syntax error by the end=""
How can I fix this?
Solution
You are using python 2 not python 3 , you can either use:
from __future__ import print_function # import the print function
Or use:
print(userString * val), # <- trailing comma
You could also use join and a list comprehension to match the output of your python 3 print code:
val = 5
print(" ".join(["*" * val for _ in range(val)])) if val else ""
***** ***** ***** ***** *****
Answered By - Padraic Cunningham
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.