Python Forum
if-elif-else statement not executing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if-elif-else statement not executing
#1
Hi everyone, I'm new to programming so sorry in advance if my mistake is a stupid one.
I am having trouble making my if-elif-else statement execute properly in my dice rolling simulator, it always defers to the statement for the else part, no matter the input. For example, if the user inputs 1 to play again, they will be told the input is not valid. Same thing if they input 2 in order to stop playing.
Here is my code:

from random import randint


def diceRollingSimulator():
    dice = randint(1, 6)
    print(dice)


diceRollingSimulator()
answer = input("Would you like to play again? (enter 1 for yes or 2 for no): ")
if  answer == 1:
    diceRollingSimulator()
elif answer == 2:
    print("Thanks for playing!")
else:
    print("Not a valid input!")
Reply
#2
input() will return str. In your if/elif conditions you compare str to int. so it's never True and else clause is executed.
You can either convert answer to int e.g. answer = int(input(...)) or alterntaively, compare answer to str, e.g. if answer == '1':
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The input() function always returns a string. Because strings are not numbers, they cannot be compared. To fix this, you need to cast the input to a number type:

from random import randint
 
 
def diceRollingSimulator():
    dice = randint(1, 6)
    print(dice)
 
 
diceRollingSimulator()
answer = int(input("Would you like to play again? (enter 1 for yes or 2 for no): "))
if  answer == 1:
    diceRollingSimulator()
elif answer == 2:
    print("Thanks for playing!")
else:
    print("Not a valid input!")
Reply
#4
Thank you!
Reply
#5
answer = int(input("Would you like to play again? (enter 1 for yes or 2 for no): "))
Note that it will raise an exception if input is not valid for conversion to int with base 10.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question If, elif, and else statement not working PickleScripts 3 841 Mar-30-2023, 02:53 PM
Last Post: PickleScripts
  If elif else statement not functioning SamDiggityDog 4 2,601 Jun-03-2020, 12:27 AM
Last Post: SamDiggityDog
  Proper use of if..elif..else statement nick1941 2 2,376 Mar-06-2020, 11:22 PM
Last Post: nick1941
  Syntax Error (elif statement) Kanashi 0 3,638 Nov-20-2019, 11:29 PM
Last Post: Kanashi
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,217 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Problem with elif statement Haddal99 2 2,228 May-20-2019, 09:26 AM
Last Post: avorane
  if /else statement not executing correctly bluethundr 3 2,731 Apr-15-2019, 10:20 PM
Last Post: bluethundr
  Unexpected input within an if/elif statement fier259 2 2,779 May-12-2018, 07:41 AM
Last Post: buran
  'else' statement not executing sparkz_alot 3 3,827 Jul-20-2017, 01:00 AM
Last Post: ichabod801
  print() statement not executing.. bmohanraj91 3 3,637 May-01-2017, 03:56 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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