有麻烦:元组和整数? [英] Having Trouble With: Tuple and Int?

查看:124
本文介绍了有麻烦:元组和整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在做一个骆驼游戏,但出现一个奇怪的错误,说

So I am making a camel game and I get a weird error that says

在'tuple'和'int'的实例之间不支持TypeError:'>'"

"TypeError: '>' not supported between instances of 'tuple' and 'int'"

我不确定这意味着什么,我之前已经发布过,我将再次声明我是初学者.谢谢任何帮助这里代码的人,我将在错误在于您的地方加一个注释!

and I am not sure what this means, I have posted before and I will state this again I am a beginner coder. Thank you to whoever helps here is the code and I will put a comment where the error is that you!

        import random
    done = False
    milesTraveled = 0
    thirst = 0
    camelTired = 0
    nativesDis = -20
    canteens = 5
    print('''
    Welcome to Camel!
    You have stolen a camel to make your way across the great Mobi desert.
    The natives want their camel back and are chasing you down! Survive your
    desert trek and outrun the natives. ''')
    while not done:
        oasis = (1,21)
        if oasis == 4:
            print("You found an oasis!")
            canteens = 5
            thrist = 0
        if thirst > 6:
            print("You died of thrist!")
            spc = input("")
            done = True
        if thirst > 4:
            print("You are thirsty")
#under here
        if camelTired > 8:
            print("Your camel DIED!")
            spc = input("")
            done = True
        if camelTired > 5:
            print("Your camel is getting tired")
        if nativesDis == milesTraveled + 20:
            print('The natives caught up with you!')
            spc=input("")
            done = True
        if milesTraveled == 200:
            print('You Win!')
            spc = input("")
            done = True
        print('''
        A. Drink from your canteen.
        B. Ahead full speed.
        C. Stop and rest.
        D. Status check.
        Q. Quit ''')
        user_choice = input("")
        if user_choice.upper() == 'Q':
            done = True
        elif user_choice.upper() == 'D':
            print('''
            Miles Traveled: 0
            Drinks In Canteen:''',canteens,'''
            Thirstyness:''',thirst,'''
            The Natives Are 20 Miles Behind You''')
        elif user_choice.upper() == 'C':
            camelTired = 0
            nativesDis = random.randint(7,15)
        elif user_choice.upper() == 'B':
            milesTraveled = random.randint(10,22)
            print("You traveled",milesTraveled,"miles!")
            thirst + 1
            camelTired = (1,3)
            nativesDis = (7,14)
        elif user_choice.upper() == 'A':
            if canteens > 0:
                canteens = canteens - 1
                thirst

= 0

推荐答案

您需要将数字从元组中取出,并放入一个也是整数的变量中.

You need to take the number out of the tuple and into a variable that is also an integer.

元组中有多个整数.

您可以通过以下方式访问它们:

You can access them in the following way:

some_tuple_of_integers = (12, 432, 345)

index_zero_integer = some_tuple_of_integers[0]
index_one_integer = some_tuple_of_integers[1]
index_two_integer = some_tuple_of_integers[2]

print(index_zero_integer)
print(index_one_integer)
print(index_two_integer)

或者直接从元组本身而不创建新变量(当使用大量索引和元组时,有时会变得不可读).

Or just straight from the tuple itself without creating a new variable (This can sometimes get unreadable when working with lots of indexes and tuples).

print(some_tuple_of_integers[0])
print(some_tuple_of_integers[1])
print(some_tuple_of_integers[2])

然后您可以轻松地在其他值之间进行比较.

You can then easily compare between other values.

例如,如果您有一个元组中的字符串需要与另一个整数进行比较,则可以执行以下操作来更改它:

If, for example you have a string from the tuple that you need to compare with another integer, you can change it by doing:

index_two_integer = int(index_two_integer)

这篇关于有麻烦:元组和整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆