Fizzbuzz (translating my output to a string?)

New here and I hope that I can help as much as I will help. Basically, I was tasked with writing a fizzbuzz program in Python and so far so good, except for some of the feedback I received.

Now I have to make sure that the output of my program intersects horizontally and is not printed vertically on new lines. In my opinion, and my lecturers are hinting that I need to enable a function to create lines and also delete print instructions. A.

Code below:

def fizzbuzz1 (num):
    for num in range(1, num): 
        if (num%3 == 0) and (num%5 == 0): 
             print("Fizzbuzz")
        elif ((num % 3) == 0):
             print("Fizz")
        elif ((num % 5) == 0):
             print("buzz")
        else :
             print (num)

def main (): 
    while True: #Just to keep my program up and running while I play with it
    num = input ("please type a number: ") #
    num = int (num)
    print ("Please select an type what option you wish to try: A) Is this Fizz or Buzz? B) Count saying fizz/buzz/fizzbuzz") 
    opt = input ("Please type A or B and press enter: ")
    if opt == "A": 
        fizzbuzz(num)
    elif (opt == "a")
        fizzbuzz(num)
    elif (opt == "B"):
        print (fizzbuzz1(num))
    elif (opt == "b"):
        print (fizzbuzz1(num))

main ()

I have tried a number of things, and my lecturer does not seem to be too interested in helping me. WOMP. I was encouraged to consider this exercise when I played with this piece of code:

def func(num):
value = ‘’
for x in range(...):
    if   .... == .... :
        value += str(x) + ‘, ‘
return value[…]# You need to remove the last comma and the space

, , . , . ?

. , , .

.

: , thimgs, , !

: , python?

.

+4
2

. , , , , join , . :

>>> def fizzbuzz1(num):
...     fb = []
...
...     # Iterate through the range, then use `fb.append()` to append the
...     # new element to the end of the list.
...     for n in range(1, num):
...         if not (n%3 or n%5):
...             fb.append('Fizzbuzz')
...         elif not n%3:
...             fb.append('Fizz')
...         elif not n%5:
...             fb.append('Buzz')
...         else:
...             fb.append(str(n))
...
...     return ', '.join(fb)
...
>>> fizzbuzz1(20)
'1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizzbuzz, 16, 17, Fizz, 19'
>>>
+1

, Python 3.x

def fizzbuzz1 (num):
for num in range(1, num): 
    if (num%3 == 0) and (num%5 == 0): 
        print("Fizzbuzz ", end="")
    elif ((num % 3) == 0):
        print("Fizz ", end="")
    elif ((num % 5) == 0):
        print("buzz ", end="")
    else:
        print ( str(num) + " ")
print(" ")
+1

All Articles