0

I'm new to coding and this is my first post here so I hope I am doing it correctly.

I am learning Python and trying to do the "FizzBuzz" program. I am trying to implement a string asking the user if they want to continue, with a "y" or "n" response, if "y" it keeps running, if "n" it simply says 'Thank you for playing".

I just cannot seem to get the latter response to work. Any help would be greatly appreciated. Here is my code (from PyCharm):

valid_input = True


def number_game():
    num = int(input("Please type a whole number "))

    if (num % 3 == 0) and (num % 5 == 0):
        print("Fizz Buzz")
    elif num % 3 == 0:
        print("Fizz")
    elif num % 5 == 0:
        print("Buzz")
    else:
        print(num)

    cont = input("Continue? Y or N").lower()
    if cont == "y":
        number_game()
    else:
        print("Thank you for playing")


# Should I use a while loop somewhere in code instead?

    # while valid_input:
    #     number_game()
    #     print("\n")
martineau
  • 119,623
  • 25
  • 170
  • 301
Pesto
  • 1
  • 1
  • 1
    short answer: your code is valid, but yes, I'd recommend to use a loops, if you can avoid recursion, in general. This way, you don't clutter what is called "the stack".I'll others elaborate or yourself searching about it. Also, I also find loops a bit more readable, but that's my opinion. – Pac0 Aug 20 '21 at 18:28
  • your code does exactly what you claim to want it to do ... – Joran Beasley Aug 20 '21 at 18:29
  • Thank you both. When I run the code in PyCharm I get nothing. It doesn't say "Please type a whole number ". – Pesto Aug 20 '21 at 18:52
  • Hi @Pesto you commented out the last part. You need to invoke the function somewhere. I can't answer you anymore though... – SvenTUM Aug 20 '21 at 18:58
  • 1
    Ok, makes sense - thank you! I never wrote in the last line of code to start it all: number_game() – Pesto Aug 20 '21 at 18:59

0 Answers0