Python Forum

Full Version: input issue elif
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code I am writing: Write code that iterates through number 1 - 20 and prints “Fizz” if it’s a multiple of 3, “Buzz” if it’s a multiple of 5, “FizzBuzz” if it’s a multiple of 3 and 5, and the number if it’s not a multiple of 3 or 5. It should only print one statement per number.

x=input ("Please Type a Number from 1 to 20.")
if x%3 == 0 and x%5 == 0:
print ("FizzBuzz")
elif x%3 == 0:
print ("Fizz")
elif x%5 == 0:
print ("Buzz")
else:
print ("Your Number is " + str(x))


My problem - when it executes it is skipping the first three statements and printing Your number is for all numbers.
The issue is that the input you receive from the user is a string. So when the console prints "Please Type a Number from 1 to 20" and the user enters (say) 15, the value given to x is '15' rather than the number 15. You could solve this by changing the first line to:

x = int(input("Please Type a Number from 1 to 20."))
You have to input an integer on the first line x=int(input))