`>`在Python中使用“input()”时,“str”和“int”之间不支持

2024-04-27 19:49:30 发布

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

我正在写一个个人代码,我似乎遇到了一个障碍。代码应该运行,但是没有。我把它复制粘贴在下面让你们看,还有错误。我知道这是一个简单的错误,但我不明白这是什么意思

print(contents1)

print(contents2)
choose = input("So, what is your answer?")
if choose == 1:
    print(contents3)
else:
    if choose == 2:
        print(contents4)
        number = random.randint(1, 6)
        if number >= 3:
            print(contents5)
        else:
            print(contents6)
            number2 = random.randint(1, 6)
            if number2 >= 2:
                print(contents7)
            else:
                print(contents8)
                useItem1 = input("So, do you use the Holy Hand Grenade, or save "
                                 "it for later?")
                if useItem1 == 1:
                    print(contents9)
                    holy_hand_grenade = holy_hand_grenade - 1
                elif useItem1 == 2:
                    print(contents10)
                else:
                    print(contents11)
                    useItem1 = input("Once again, do you use the Holy Hand Grenade, or save "
                                 "it for later?")
                    if useItem1 == 1:
                        print(contents9)
                        holy_hand_grenade = holy_hand_grenade - 1
                    elif useItem1 == 2:
                        print(contents10)
                    else:
                         print(contents12)
                         sys.quit()
    else:
        if choose == 3:
            print(contents13)
        else:
            if choose > 3:
                print(contents11)
                if choices == 1:
                    print(contents3)
                else:
                    if choose == 2:
                        print(contents4)
                        number = random.randint(1, 6)
                        if number >= 3:
                            print(contents5)
                        else:
                            print(contents6)
                            number2 = random.randint(1, 6)
                            if number2 >= 2:
                                print(contents7)
                            else:
                                print(contents8)
                                useItem1 = input("So, do you use the Holy Hand Grenade, or save "
                                 "it for later?")
                                if useItem1 == 1:
                                    print(contents9)
                                    holy_hand_grenade = holy_hand_grenade - 1
                                elif useItem1 == 2:
                                    print(contents10)
                                else:
                                    print(contents11)
                                    useItem1 = input("Once again, do you use the Holy Hand Grenade, or save "
                                     "it for later?")
                                    if useItem1 == 1:
                                        print(contents9)
                                        holy_hand_grenade = holy_hand_grenade - 1
                                    elif useItem1 == 2:
                                        print(contents10)
                                    else:
                                        print(contents12)
                                        sys.quit()
            else:
                print(contents12)
                sys.quit()
Traceback (most recent call last):
  File "C:\Users\Spencer\Documents\School\Semester #13\CIS-105\Final Project\boss_fight.py", line 138, in <module>
    first_boss()
  File "C:\Users\Spencer\Documents\School\Semester #13\CIS-105\Final Project\boss_fight.py", line 97, in first_boss
    if choose > 3:
TypeError: '>' not supported between instances of 'str' and 'int'

我非常感谢你的帮助。此外,一些建议,以简化它将是非常受欢迎的


Tags: younumberinputifrandomdoelseprint
2条回答

另外,我建议您使用

elif choose == 2:

而不是

else: 
    if choose == 2: 

可能您正在使用python3:input()返回一个字符串,raw_input()已被弃用。相反,在python2中,input()返回一个数值。有关详细信息,请参阅文档的Built-In Functions部分

你需要做一些像

...
print(contents2)
choose = int(input("So, what is your answer?")) # Notice the `int()` here
...

或者如果你得到很多数字,使用

choose_map = map(int, input("So, what are your answers?").split())
choose_list = list(choose_map)

如果您使用的是python2,并且仍然存在这个问题,请提供import的列表。可能有什么不对劲

相关问题 更多 >