遇到麻烦:元组和国际?

时间:2018-03-17 00:28:34

标签: python python-3.x int tuples

所以我正在制作一个骆驼游戏,我得到了一个奇怪的错误,上面写着

  

“TypeError:'>' 'tuple'和'int'实例之间不支持

而且我不确定这意味着什么,我之前已经发布了,我将再次说明这是一个初学者编码器。谢谢你这里有帮​​助的是代码,我会在你错误的地方发表评论!

        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

1 个答案:

答案 0 :(得分:2)

您需要将数字从元组中取出并转换为也是整数的变量。

元组中有多个整数。

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

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)

或者直接从元组本身直接创建一个新变量(在使用大量索引和元组时,这有时会变得难以理解)。

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

然后,您可以轻松比较其他值。

例如,如果你有一个来自元组的字符串,你需要与另一个整数进行比较,你可以通过这样做来改变它:

index_two_integer = int(index_two_integer)