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

时间:2017-11-30 02:52:24

标签: python

我正在编写个人代码,我似乎遇到了麻烦。代码应该运行,但事实并非如此。我已经复制并粘贴在下面供你们查看,并且错误。我知道这是一个简单的错误,但我无法弄清楚它的含义。

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

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

2 个答案:

答案 0 :(得分:1)

可能您使用的是Python 3:raw_input()返回一个字符串,并且不推荐使用input()。相反,在Python 2中,... print(contents2) choose = int(input("So, what is your answer?")) # Notice the `int()` here ... 返回了一个数值。有关详细信息,请参阅文档的Built-In Functions部分。

您需要执行类似

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

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

import

如果您使用的是Python 2,但仍有此问题,请提供您的subprocess列表。那里可能有东西。

答案 1 :(得分:0)

另外,我建议你使用

elif choose == 2:

而不是

else: 
    if choose == 2: