TypeError: unsupported operand type(s) for Sub: ‘list’ and ‘int’ on line 39

  python-3.x

This is my code:

import random
#Amount of turns you have. (4 = Hard; 6 = Medium; 8 = Easy)
turns = [4, 6, 8]
difficulty = input("Choose difficulty (Easy, Medium, Hard): ")
if difficulty == "Easy":
    print(str(turns[2]) + " turns left!")
elif difficulty == "Medium":
    print(str(turns[1]) + " turns left!")
elif difficulty == "Hard":
    print(str(turns[0]) + " turns left!")
else:
    print("Invalid difficulty! (Easy, Medium, Hard)")
    print(" ")

# This is the list of numbers that the user 
# is trying to find.
list_num = [[2, 7, 1, 5], [4, 1, 9, 0], [5, 0, 3, 2], [8, 6, 2, 3]]
secret_num = random.choice(list_num)

while turns != 0: #Stops when turns reaches 0.
    try:
        user_number = int(input("Enter a number: "))
        #Checks if the number that the user enter a number that is between 0 and 10.
        if user_number < 11 and user_number > -1:
            if user_number in secret_num:
                print("Found it!")
                #If the user matches one of the numbers in the list, the for loop
                #will check and remove that number from the list.
                for user_number in secret_num: 
                    secret_num.remove(user_number)#will check and remove that number from the list.
                    print(str(len(secret_num))+ " left!")#Tells you how many numbers you have left.
                    print(" ")
                    break
                if len(secret_num) == 0:#Once you have 0 nums left in the list it will print this:
                    print("Congrats! You have found all of the numbers!")
                
            elif user_number not in secret_num:#Did not find the number. Results: -1 turn.
                print("Wrong number! Hint: There are " +str(len(secret_num))+ " numbers to find.")
                ** turns -= 1 **
                print("You have " +str(turns)+ " left!")
                continue
        else: #Will only print if the user num is > -1 and < 11.
            print("Integer must be in between 1 and 10!")
            print("")
            continue
    except ValueError: #If the user types a letter, it will print this and a penalty: -1 turn.
        ** turns -= 1 **
        print("That was not an integer!")
        print("You have " +str(turns)+ " turns left!")
        print(" ")
        continue
print("Game Over:") #Only prints if the turn value reaches 0.
print("You ran out of turns!")

My problem with this code is that every time the user enters a number that is not in the random list or enters a letter, the turn should decrease by one. * This is where the problem is. *

This should run by the following steps:
1)Enter difficulty.
2)Python selects a random list.
3)User enters a number that is < 11 and > -1.
3a)If the user enters the number that’s in the list, it will enter a for loop and will remove the number that the user has entered. Otherwise, it will print ("Wrong number! Hint: there are ___ numbers left.") and should decrease the turn value by one.
3b) If the user enters a letter, it will print ("Must be an integer!") and should also decrease the turn value by one, and tells the user how many turns are left.
4) If the user runs out of turns, it print("Game Over:").
4b) If the len(list_num) == 0, it should print ("Congrats! You have found all of the numbers!").

Source: Python-3x Questions

LEAVE A COMMENT