我在这里遇到一些代码有些问题。它的python,我在终端运行它。这是代码。
def inputPlayerLetter():
print('Do you want to be X or O?')
letter = input()
if letter == "X":
print "You are now X's, you will go first "
Player = X
Computer = O
else:
print "You are now O's you will go second"
Player = O
Computer = X
然而,当我在终端启动文件时,我得到了这个
HHNEs-MacBook-Pro-3:ttt hhneadmin$ python ttt.py
Do you want to be X or O?
X
You are now O's you will go second
HHNEs-MacBook-Pro-3:ttt hhneadmin$ !!
python ttt.py
Do you want to be X or O?
O
You are now O's you will go second
为什么说当我选择X时,我仍然得到应该是O时得到的响应。这可能是一个愚蠢的问题,但我是python的初学者,所以请帮助一起解释一下。
答案 0 :(得分:0)
我认为这是你要做的事情:
def inputPlayerLetter():
letter = raw_input('Do you want to be X or O? ')
if letter == "X":
print "You are now X's, you will go first"
Player = 'X'
Computer = 'O'
else:
print "You are now O's you will go second"
Player = 'O'
Computer = 'X'
inputPlayerLetter()
<强>输出强>
Do you want to be X or O? X
You are now X's, you will go first
您的代码存在问题player = X
。由于X
不是字符串,因此pyhton将其视为变量并查找X
的定义。由于X
未在任何地方定义,因此会引发异常。