python - how do i properly make a for loop sit on one line, with commas and no spaces -
i'm new coding , it's little frustrating @ moment because have assessment outputs "1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz,16,17,fizz,19,buzz" , no cant use print.
code:
import sys num1=int(input('enter number range: ')) x in range(1,num1+1): if x % 3 == 0: print("fizz",end=" ") if x % 5 == 0: print('buzz',end=" ") if x % 3 != 0 , x % 5 != 0: print(str(x),end=" ")
result: "enter number range?"(well 20)
1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizz buzz 16 17 fizz 19 buzz
so can see on same line don't want spaces, , want commas in between each number , letter accept number 15 should be, want 1 word "fizzbuzz", seen on example :
1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizzbuzz,16,17,fizz,19,buzz
this same question have answered here. if @ documentation print, says print variable number of objects (*objects
in docs). print
takes sep
keyword, character(s) want separate printed objects with.
if modify code collect items-to-be-printed in list first , print @ once:
num1=int(input('enter number range: ')) items = list() x in range(1,num1+1): if x % 3 == 0: items.append("fizz") if x % 5 == 0: items.append('buzz') if x % 3 != 0 , x % 5 != 0: items.append(str(x)) print(*items, sep=',')
when run it:
enter number range: 10 1,2,fizz,4,buzz,fizz,7,8,fizz,buzz
for *objects
-syntax can take @ so-question.
to account numbers evenly divisible both 3 , 5:
num1=int(input('enter number range: ')) items = list() x in range(1,num1+1): if x % 5 == 0 , x % 3 == 0: items.append('fizzbuzz') elif x % 3 == 0: items.append("fizz") elif x % 5 == 0: items.append('buzz') else: items.append(str(x)) print(*items, sep=',')
Comments
Post a Comment