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.

1
  • Note that I fixed your indentation. Commented Nov 27, 2016 at 21:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

For validating inputs using Python take a look at: Asking the user for input until they give a valid response
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.