Having Trouble With: Tuple and Int?

2020-05-07 10:00发布

问题:

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

"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

回答1:

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

There are multiple integers in your tuple.

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)