0
while True:
    num=raw_input("Please enter a number.")
    if (num == 1):
        print "Sunday"
    elif (num==2):
        print "Monday"
    elif (num==3):
        print "Tuesday"
    elif (num==4):
        print "Wednesday"
    elif (num==5):
        print "Thursday"
    elif (num==6):
        print "Friday"
    elif (num==7):
        print "Saturday"
    else:
        print "Invalid Choice!"

    option = raw_input("Would you like to continue playing?")
    if (option=="yes"):
        continue
    elif (option=="no"):
        break

This my code. When I run it for some reason the output for the first part (The day's of the week) come up as the "else" option which is "Invalid Choice". And when I removed the else statement, the output was just blank. Slightly confused as to why this is happening.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
mars
  • 49
  • 5

2 Answers2

5

raw_input will return the input as str. You have to convert it to int if you want to use it as a conditional for the if statements.

num=int(raw_input("Please enter a number."))

Note that if the user doesn't input a number, this will raise an error.

Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
  • For validating inputs using Python take a look at: [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – dot.Py Nov 27 '16 at 21:23
1

When you're building the program, I'd recommend you add more information to your "Invalid choice!" responses. For example:

print("You entered `{entry}` ({entry_type}), which is an invalid choice!".format(entry=num, entry_type=type(num)))

would have told you

You entered `3` (<class 'str'>), which is an invalid choice!

which might suggest you either need to be comparing to strings, or casting your input to an integer.

Bryant
  • 622
  • 4
  • 18