简体   繁体   中英

Python raw_input issue

Here's my code

def player_input():
    input = raw_input('Choose X or O')
    if input == "X":
        print input
    else:
        print 'Please choose an X or an O'
        player_input()

player_input()

Not sure why I'm getting this. No matter what I type, it seems to jump to the else statement.

Please Choose an X or an O
Choose X or O>? l
Please Choose an X or an O
Choose X or O>? X
Please Choose an X or an O
Choose X or O

To your second question answered in the comments

Thanks all! Turns out it was an issue with PyCharms interpreter :( As soon as I used the python interpreter on my OS it came out fine. My next issue is using the 'or' comparison. This -> if input == "x" or "X" or "o" or "O": yields the result of only accepting lowercase x. Everything else jumps to the else statement. including X o and O – jmleczko

In python you have to use

if input == 'x' or input == 'X' or input == 'o' or input == 'O':

Python does not like it otherwise. I recommend not to use input as a variable, since input is a system reserved function.

someInput = input("Enter here:")
>>> Enter here: ABC
print(someInput)
>>> ABC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM