有人能告诉我为什么我的代码不起作用吗?

2024-09-20 22:56:53 发布

您现在位置:Python中文网/ 问答频道 /正文

numCat = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
print("Hello user, I am Inverted Cheese.\n")
username = input("What is your name?\n").capitalize()
print("Hello, " + username + "!")

while True:
    if input() == "hello":
        print("Hello, " + username + "!")
    elif input() == "cat":
        num = randrange(0,10)
        print(num)
        if num == 0:
            numcat[num] += 1
            print("You now have " + numcat[num] + "brown cats")
        elif num == 1:
            numcat[num] += 1
            print("You now have " + numcat[num] + "grey cats")
        elif num == 2:
            numcat[num] += 1
            print("You now have " + numcat[num] + "white cats")
        elif num == 3:
            numcat[num] += 1
            print("You now have " + numcat[num] + "black cats")
        elif num == 4:
            numcat[num] += 1
            print("You now have " + numcat[num] + "orange cats")
        elif num == 5:
            numcat[num] += 1
            print("You now have " + numcat[num] + "furless cats")
        elif num == 6:
            numcat[num] += 1
            print("You now have " + numcat[num] + "hairy cats")
        elif num == 7:
            numcat[num] += 1
            print("You now have " + numcat[num] + "small cats")
        elif num == 8:
            numcat[num] += 1
            print("You now have " + numcat[num] + "fat cats")
        elif num == 9:
            numcat[num] += 1
            print("You now have " + numcat[num] + "chubby cats")
        elif num == 10:
            numcat[num] += 1
            print("You now have " + numcat[num] + "magic cats")
        print(num)
    elif input() == "inventory1":
        print("hi)")
        print("Inventory Part I:\n\nCommon Cats:\nBrown: " + numCat[0] + "\nGrey " + numCat[1] + "\nWhite: " + numCat[2] + "\nBlack: " + numcat[3] + "\nOrange: " + numCat[4] + "\n\nRare Cats:\nFurless: " + numCat[5])
        print("hi)")
    elif input() == "inventory2":
        print("hi)")
        print("Inventory Part II:\n\nRare Cats:\Hairy: " + numCat[6] + "\nSmall: " + numCat[7] + "\n\nEpic Cats:\nFat: " + numCat[8] + "\nChubby: " + numCat[9] + "\n\nLegendary:\nMagic: " + numCat[10])
        print("hi)")

input()

唯一有效的命令是“hello”命令,但是“cat”和两个inventory命令都不起作用。当我尝试使用它们时,我没有得到任何输出。有人能解释一下为什么吗?你知道吗

它也不会打印任何东西。我在清单代码中添加了print命令,但它们不会打印。我的单子有什么问题吗?你知道吗

我真的很想运行这个程序,但我完全迷路了。你知道吗


Tags: 命令youhelloinputhaveusernamehinow
2条回答

每次对input()的调用都需要用户的另一个输入。您应该调用input()一次,并将返回值存储在一个变量中进行比较:

while True:
    command = input()
    if command == "hello":
        print("Hello, " + username + "!")
    elif command == "cat":
        num = randrange(0,10)
        print(num)
        ...

您将在每个elif评估中读取新的输入。将输入存储一次,以便重复比较:

i = input()
if i == "hello":
    print("Hello, " + username + "!")
elif i == "cat":
    # ...
elif i == ...:
    # ...

相关问题 更多 >

    热门问题