-3

I am creating a fizzbuzz game and i would like to create a function that allows the user to input a new rule into the code when it reaches 21. If anyone has any solutions i would be very grateful. Btw I am a year 10 student so a simple solution would be preferred but it's ok if it is complicated

This is my current code

count = 0
while count <= 21:
    if count %3==0 and count%5==0:
        print("FizzBuzz")
    elif count %3==0:
        print("Fizz")  
    elif count %5==0:
        print("Buzz") 
    else:
        print(count)
    count=count+1
polarise
  • 2,303
  • 1
  • 19
  • 28
  • 2
    You can use `input()` to get input from the user and `eval()` to run it as code. Be aware that `eval()` on user input is dangerous, though. It is probably okay to use in a toy example like this, but should only be used with EXTREME caution because it causes a security risk. – Code-Apprentice Jun 12 '23 at 21:21
  • 2
    You should be aware of the dangers of using `eval`; see [link](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html). The ast module has a function `literal_eval` which is much safer. – user19077881 Jun 12 '23 at 21:31

1 Answers1

-1

10 years old or studying for 10 years?

make function and call it with and argument. then ask the use for input and call it again with their number. this is pretty basic and there is not error checking the input.

def fizz_buzz(number):
    for count in range(1, number + 1):
        result = ''
        if count % 3 == 0: result += 'Fizz'
        if count % 5 == 0: result += 'Buzz'
        print(count, ':', result)

fizz_buzz(21)

new_rule = int(input('enter another number: '))

fizz_buzz(new_rule)
richard
  • 7
  • 1