How can I create a function that will allow a user to input a piece of code

51 Views Asked by At

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
1

There are 1 best solutions below

0
richard On

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)