Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input issue elif
#1
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.
Reply
#2
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."))
Reply
#3
You have to input an integer on the first line x=int(input))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue with program not passing to other elif condition Abdirahman 6 2,097 Nov-21-2021, 07:04 AM
Last Post: Abdirahman
Big Grin Newbie to Python - input and branching issue Sherine 3 2,228 Jul-31-2021, 01:09 AM
Last Post: Pedroski55
  Experiencing a bug probably related to input() issue NewtoPython2020 1 1,911 Dec-05-2020, 08:13 AM
Last Post: perfringo
  Using Input() in If...Elif Statements Shemira 11 7,835 Nov-24-2019, 06:57 AM
Last Post: perfringo
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,237 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  an input error or elif error , how can i fix this the_fire_pharaoh 1 2,344 Dec-15-2018, 09:47 PM
Last Post: Gribouillis
  Mixed string,Integer input variable issue maderdash 2 2,745 Nov-06-2018, 09:46 AM
Last Post: snippsat
  Unexpected input within an if/elif statement fier259 2 2,809 May-12-2018, 07:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020