4

New here and hoping I can help as much as I am helped. Basically, I have been tasked with writing a fizzbuzz program in Python and so far so good except for some feedback I received.

Now I have to ensure that the output of my program comes across horizontally and is not printed vertically on new lines. From my understanding, and my lecturers hinting, I need to turn function to produce strings and remove the print statements as well.

Code is 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 whole host of things, and my lecturer doesn't seem too interested in help me. Womp. I was recommended to review this exercise were 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

When I do play with this code, I do get numbers to go across the screen. But for the life of me I can not seem to incorporate what I have written with elements from this. Where am I going astray?

Thank you for any and all your advice/help. If you do choose to reply, please keep it as simple as possible for me.

Cheers.

Update: Thanks everyone for your suggestions, lots of thimgs I didnt know to try!

I also found a thread here at: Can't figure out how to print horizontally in python?

Which has answers to a similar issue.

Community
  • 1
  • 1
Buggs
  • 41
  • 4

2 Answers2

1

You're very close. The approach I would take would be to store the results in a list, then join the contents of the list to make the output string. Here's an example:

>>> 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'
>>>
Deacon
  • 3,615
  • 2
  • 31
  • 52
  • Tyvm, I will try this. I havent been taught thay yet so if I oull it off I will have to make sure I can explain it. Again, ty. – Buggs Mar 08 '16 at 16:58
  • 1
    If you can't use join a do a for each loop and append each item in the list and ", " @Buggs – ford prefect Mar 08 '16 at 17:08
  • Hi Doug R, your suggestion did the trick for me. I will have to learn mour about the .append however in order to explain how it actually works. – Buggs Mar 09 '16 at 05:16
  • @Buggs - Glad to help. [`list.append()`](https://docs.python.org/3.5/tutorial/datastructures.html) does exactly what it sounds like--it appends a new element to the end of the list. You can also accomplish the same thing by changing `fb.append('Fizzbuzz')` to `fb += ['Fizzbuzz']`, but it's slower and a less Pythonic way to do it. There's a discussion of the two methods [here](http://stackoverflow.com/a/725882/1843248). – Deacon Mar 09 '16 at 13:25
1

Try printing without a new line, if you use 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(" ")
Yunhe
  • 665
  • 5
  • 10
  • Hi Yunhe, That did the trick. Amazing. But can I ask, how does the , and end = "" actually make it work? – Buggs Mar 09 '16 at 05:11
  • Also, I am unsure if this will fly with my lecturer as the print statement is still within the function (He doesn't want it in there), however I am very, very impressed with it myself. However thanks again, this really did help me. – Buggs Mar 09 '16 at 05:18
  • print is treated as a function in Python 3.x and end = "" means the line ends with "" rather than a linebreak. Hope it helps! – Yunhe Mar 09 '16 at 15:36