I'm trying to restrict the users input to the selected characters only.
"W" "w" or "L" "l"
Anything else entered, an error message should display.

Can anyone please look at this and tell me what I'm doing wrong?
Thank you

# User Enters either W = Win and L = Loss
# If user types in other W w, Ll display error will display
def main():
    print "Please use W or L to input the results of the game"
    print
    win_loss_code = raw_input('Enter W for Win or L for Loss: ')
    if win_loss_code == 'W' or 'w':
        print "You won"
    if win_loss_code == 'L' or 'l':
        print "you lost"
    else:
        print "Error"
        print "Please use only W or L"
    
main()

Recommended Answers

All 6 Replies

The statement if win_loss_code == 'W' or 'w': doesn't parse the way you want. Try if win_loss_code == 'W' or win_loss_code == 'w': and do the corresponding for Ll.

The way it is currently written the test is

  1. Is win_loss_code equal to 'W'? *OR*
  2. Is the string 'w' a non-NULL string?

Since case [2] is always true, the if doesn't get any farther.

commented: great explanation. I did not see that 'w' part. +3

I wasn't sure how to combine both the 'W' and 'w" in one line.
Thank you for the tip, it worked like a charm.

If the user, inputs a character other than 'W' , 'w" or 'L', 'l'
how do I display and error message?

You can use a while loop, something along the lines of

found = 0
while not found:
    print "Please use W or L to input the results of the game"
    print
    win_loss_code = raw_input('Enter W for Win or L for Loss: ')
    win_loss_code = win_loss_code.lower()
    if win_loss_code == "w":
        print "You won"
        found = 1
    elif win_loss_code == 'l':
        print "you lost"
        found = 1
    else:
        print "Error"
        print 'Please use only "W" or "L" '
##---------------------------------------------------------------------
## if you have a lot of letters to test, use a dictionary
print_dic = {"w":"You won", "l":"You lost", \
                    "x":"Stupid, no one uses x" }
found = 0
while not found:
    print "Please use W or L to input the results of the game"
    print
    win_loss_code = raw_input('Enter W for Win or L for Loss: ')
    win_loss_code = win_loss_code.lower()
    if win_loss_code in print_dic:
        print print_dic[win_loss_code]
        found = 1
    else:
        print "Error"
        print 'Please use only "W" or "L" '

There are a lot of variations on this, including the use of a function for input, but this is the general idea.

That was impressive solution.
Question:

found = 0
while not found:

What do these two lines do?

I generally find it easier to use an endless while loop and break out:

# set up an endless loop ...
while True:
    print "Please use W or L to input the results of the game"
    print
    win_loss_code = raw_input('Enter W for Win or L for Loss: ')
    win_loss_code = win_loss_code.lower()
    if win_loss_code == "w":
        print "You won"
        break    
    elif win_loss_code == 'l':
        print "you lost"
        break
    else:
        print "Error"
# now you are out of the loop so keep going ...

I fully agree, Vega. The coding purists frown on while True loops, but I find them really useful. Glad to know someone else out there shares my guilty secret. :lol:

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.