【问题标题】:why does it not allow me to print "player 1 is x"为什么它不允许我打印“玩家 1 是 x”
【发布时间】:2020-09-14 03:34:07
【问题描述】:

我无法打印('玩家 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")

【问题讨论】:

  • “无法”打印该消息是什么意思?你有错误吗?
  • 请正确解释您的问题
  • 这是一个错字——缩进在 Python 中很重要,if mark == 'x': 没有正确缩进。

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


【解决方案1】:

它会给您带来任何错误吗?,我在本地机器上使用 Python 3.7 解释器运行,并且在调用方法 marker() 后它工作正常。

【讨论】:

  • 我在 Jupyter-lab 上运行它,它今天开始工作。我猜这是 Jupyter 的错误。
【解决方案2】:

这对我有用。您是否使用 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() # <---

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多