Issue
I need to print this code:
for x in range (1, 21):
if x%15==0:
print("fizzbuzz")
elif x%5==0:
print("buzz")
elif x%3==0:
print("fizz")
else:
print (x)
Horizontally instead of it printing vertically, like this:
1 2 3
to
1 2 3
I am not sure how to, some help would be great. Thanks
Solution
Two options:
Accumulate a result string and print
that at the end:
result = ""
for x in range (1, 21):
if x%15==0:
result = result + "fizzbuzz "
etc...
print result
Or tell Python not to end the printed string with a newline character. In Python 3, which you seem to be using, you do this by setting the end
argument of the print
function, which is "\n"
(a newline) by default:
for x in range (1, 21):
if x%15==0:
print("fizzbuzz",end=" ")
etc...
Historical note: In Python 2, this could be achieved by adding a comma at the end of the print
statement; print "fizzbuzz",
Answered By - Junuxx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.