简体   繁体   English

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

[英]Can someone tell me why my code isn't working?

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()

The only command that works is the "hello" command, but the "cat" and both inventory commands don't work. 唯一有效的命令是“ hello”命令,但“ cat”和两个清单命令都不起作用。 When I try and use them, I get no output. 当我尝试使用它们时,没有任何输出。 Could someone please explain why? 有人可以解释为什么吗?

It also won't print anything. 它也不会打印任何内容。 I've added print commands in the inventory code, but they won't print. 我已经在清单代码中添加了打印命令,但是它们不会打印。 Is there something I'm doing wrong with my list? 我的清单有问题吗?

I really want to get this program running, but I'm completely lost here. 我真的很想让这个程序运行,但是我在这里完全迷路了。

You would be reading new input in every elif evaluation. 您将在每个elif评估中读取新输入。 Store the input once, so you can repeatedly compare it: 一次存储输入,因此您可以重复比较它:

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

Every call to input() would require another input from the user. 每次对input()调用都需要用户的其他输入。 You should call input() once and store the returning value in a variable for comparison: 您应该调用一次input()并将返回值存储在变量中以进行比较:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以解释为什么替换对我不起作用 - Can someone explain why replace isn't working for me 我不明白为什么这段代码不起作用! 有人可以告诉我我做错了吗? - I don't see why this code is not working! can someone please tell me what i am doing wrong? 这个 python 代码对我不起作用,我不知道为什么。 告诉我代码是否有问题 - This python code isn't working for me and I do not know why. Tell me if there any promblem with the code 有人可以告诉我为什么此代码无法按预期工作吗? - could someone tell me why this code is not working as expected? python 中的变量不起作用,有人可以告诉我我的代码有什么问题吗? - Variable in python not working, can someone tell me what is wrong with my code? 有人能告诉我为什么我的 python 循环不能用于创建多项选择测验吗? - Can someone tell me why my python loop won't work for creating a multiple choice quiz? 有人能告诉我我的代码有什么问题吗 - Can someone tell me what is wrong with my code 有人可以告诉我我的 animation 代码有什么问题吗? - Can someone tell me what's wrong with my animation code? 有人可以告诉我为什么这个 python 装饰器不起作用吗? - Can someone please tell me why this python decorator is not working? 有人能告诉我为什么这段代码不起作用吗? - Can someone tell me why this code wont work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM