为什么我的“else”语句在 python 中不起作用?

克沃特

我刚刚开始创建一个刽子手游戏。我不明白为什么我的“else”语句不起作用。

这是我的代码:

while True:
    guess = input("Guess one letter in the word!: ")

    if l[0] == guess:
        print("correct")
    elif l[1] == guess:
        print("correct")
    elif l[2] == guess:
        print("correct")
    elif l[3] == guess:
        print("correct")
    elif l[4] == guess:
        print("correct")
    elif l[5] == guess:
        print("correct")
    elif l[6] == guess:
        print("correct")
    elif l[7] == guess:
        print("correct")
    elif l[8] == guess:
        print("correct")
    elif l[9] == guess:
        print("correct")
    elif l[10] == guess:
        print("correct")
    elif l[11] == guess:
        print("correct")
    else:
        print("Incorrect")
dsvp9xyjsqmfvi8p

这可能有点题外话,但你可以说:

while True:
    guess = input("Guess one letter in the word!: ")

    if guess in l:
        print("Correct")
    else:
        print("Incorrect")

这也会产生副作用,即允许您将单词更改为不同长度的猜测之一,而无需更改 if/else 链。

要知道为什么不打印 Incorrect,我们需要知道 l 的值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我的 if 语句在 python 中不起作用

为什么我的 IF 语句在 python 上不起作用?

为什么我的 python if 语句不起作用

为什么我的 else if 语句在observeEvent 中不起作用?

为什么 if/else 语句在我的函数中不起作用?

为什么条件“ else”在我的python代码中不起作用

为什么replace()在我的Python函数中不起作用?

为什么我的def函数在Python中不起作用?

为什么我的 exit() 命令在 python 中不起作用?

为什么我的 min() 函数在 Python 中不起作用

我的 if 语句在我的 Python 文件中不起作用

为什么我的if else语句不起作用?jQuery的

为什么我的if else语句(即?:)不起作用?

为什么我的jQuery if / else if语句不起作用?

为什么我的if,elseif,else语句不起作用?

为什么我的JavaScript如果else语句不起作用?

Python-我的else语句不起作用

为什么我的if-else-if语句在我的数组列表中不起作用?

为什么发送 post 请求在 python 中不起作用?

为什么 `eval` 在 Python 类函数中不起作用?

为什么嵌套的 for 循环在 Python 中不起作用?

为什么df.head()在python中不起作用

为什么短路在python的“全部”函数中不起作用?

为什么这个SQL查询在Python中不起作用?

为什么字典值比较在python中不起作用

为什么python中的sum函数不起作用?

为什么'%matplotlib inline'在python脚本中不起作用?

为什么表在Python-markdown中不起作用?

为什么这个内联循环在 python 中不起作用?