为什么不允许我打印“玩家1是x”

时间:2020-09-14 03:34:07

标签: python if-statement input while-loop boolean

我无法打印(“玩家1是x”)。我在做什么错了。

def marker():
    mark = ''
    while mark != 'x' and mark != "o":
        mark = input('select x or o:')
    if mark == 'x':
        print("player 1 is x")

3 个答案:

答案 0 :(得分:0)

这对我有效。您是否正在使用Python 3解释器运行此程序?

如果您是在Python 2.7上运行它,input()将需要在字符串输入后加上引号,这是由于在幕后在输入上调用了eval()。否则,它将把您的输入解释为名称。有关更多信息,请参见here

使用Python 2.7:

select x or o:"x"
player 1 is x

使用Python 3:

select x or o:x
player 1 is x

只是为了涵盖所有基础,您稍后将调用此marker函数,对吗?

def marker():
    mark = ''
    while mark != 'x' and mark != "o":
        mark = input('select x or o:')
    if mark == 'x':
        print("player 1 is x")

marker() # <---

答案 1 :(得分:0)

它引发任何错误吗?,我在本地计算机上运行了python 3.7解释器,并且在调用方法marker()之后运行良好。

答案 2 :(得分:-1)

它可以在我的IDE中正常运行。看来还可以。

enter image description here