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
#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


Messages In This Thread
if-elif-else statement not executing - by laila1a - Jan-05-2019, 02:03 PM
RE: if-elif-else statement not executing - by buran - Jan-05-2019, 02:15 PM
RE: if-elif-else statement not executing - by stullis - Jan-05-2019, 02:19 PM
RE: if-elif-else statement not executing - by buran - Jan-05-2019, 02:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question If, elif, and else statement not working PickleScripts 3 952 Mar-30-2023, 02:53 PM
Last Post: PickleScripts
  If elif else statement not functioning SamDiggityDog 4 2,691 Jun-03-2020, 12:27 AM
Last Post: SamDiggityDog
  Proper use of if..elif..else statement nick1941 2 2,450 Mar-06-2020, 11:22 PM
Last Post: nick1941
  Syntax Error (elif statement) Kanashi 0 3,716 Nov-20-2019, 11:29 PM
Last Post: Kanashi
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,290 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Problem with elif statement Haddal99 2 2,333 May-20-2019, 09:26 AM
Last Post: avorane
  if /else statement not executing correctly bluethundr 3 2,841 Apr-15-2019, 10:20 PM
Last Post: bluethundr
  Unexpected input within an if/elif statement fier259 2 2,879 May-12-2018, 07:41 AM
Last Post: buran
  'else' statement not executing sparkz_alot 3 3,950 Jul-20-2017, 01:00 AM
Last Post: ichabod801
  print() statement not executing.. bmohanraj91 3 3,741 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