我是python的新手,我不知道代码有什么问题

问题描述 投票:0回答:1

我刚刚创建了一个简单的函数,当我调用该函数时,当我说是时它在中间停止工作

def game():
    K = input("are you ready?")
    if K == "no":
                 return None
    if K == "yes":  
                I = input("what is 881 divisible by")
    if I == ( 1 , 881):
            print("you won")
    else:
            print("better luck next time")

我试过了,我期待它在 if K == yes 之后停止工作时工作

我不知道出了什么问题

python function findbugs
1个回答
0
投票

整个代码结构凌乱(没有适当的缩进,造成混乱) 你可能想像这样清理它:-

def game():
        K = input("are you ready?")
        if K == "no":
                return None
        elif K == "yes":  
                I = input("what is 881 divisible by")
                if I == ( 1 , 881):
                        print("you won")
                else:
                        print("better luck next time")

此外,对于

no
的情况,它应该包含在 else 部分中,因为它实际上什么都不做,所以您可以删除该部分并使其成为
if K =="yes":

def game():
        K = input("are you ready?")
        if K == "yes":  
                I = input("what is 881 divisible by")
                if I == ( 1 , 881):
                        print("you won")
                else:
                        print("better luck next time")

假设

( 1 , 881)
是正确答案,您不需要将它们设为数字而是将它们设为字符串,或者将它们包含在像
["1", "881"]
这样的列表中,然后检查
I
的值是否在列表,或者您可以使用
or
关键字

使用

["1", "881"]

...
I = input("what is 881 divisible by")
if I in ["1", "881"]:
        print("you won")
else:
        print("better luck next time")

使用

or
关键字:

...
I = input("what is 881 divisible by")
if I == "1" or I == "881":
        print("you won")
else:
        print("better luck next time")
© www.soinside.com 2019 - 2024. All rights reserved.