Rock, Paper, Scissor game. Im getting TypeError, unsupported operand type(s) for -=: 'NoneType' and 'int'

Joined
Jul 20, 2023
Messages
56
Reaction score
2
So Im writing a Rock paper scissor game and I actually had it running quite smoothly to the point that I was at. The thing is Im currently working on how to use functions. I made one change that i thought would be a better, cleaner way to write it. One simple change and it still mostly works but under a certain condtion it generates a Type Error as shown in the title. You should be able to see my origional code that was working fine. Its in comments. Basically before I was calling a "match" function from within the "check_answer" function and it was working fine. But I thought it would be better to let the "check_answer" function do its thing then go back to the main body and then call the "match" function. This line within the "match" function will generate the error under paticular condition. "user_selection -= 1" because somehow ther "user_selection" var is ending up with "None" value. This is only after the user enters an invalid selection which is determined by "check_answer(selection)" then It tells you what to enter and brings you back to the main menu. After you make the another selection is when the error occurs. What is causing the "user_selection" var to = "None"?
Python:
# import os


def clear_screen():
    # FOR CONSOLE
    print('\n' * 20)

    # Check if the operating system is Windows
    # if os.name == 'nt':
    #     os.system('cls')  # For Windows
    # else:
    #     os.system('clear')  # For other platforms like macOS and Linux


def check_answer(to_check):
    if to_check.isnumeric() and 0 < int(to_check) < 4:
        to_check = int(to_check)
        return to_check
        # match(to_check)
    else:
        clear_screen()
        print('Select a number between 1 and 3')
        input('Press ENTER to continue ')
        clear_screen()
        main_menu()


def match(user_selection):
    user_selection -= 1
    print(f'You are {rck_ppr_scssr[user_selection]}')
    print(tool[user_selection])


def main_menu():
    print('                                       Rock Paper Scissor\n')
    print('''Enter your selection:
1. Rock
2. Paper
3. Scissor\n''')

    selection = input(': ')

    selection = check_answer(selection)
    match(selection)


go_again = 'y'
rck_ppr_scssr = ('rock', 'paper', 'scissor')
tool = ["ROCK", "PAPER", "SCISSOR"]

# for x in rck_ppr_scssr:
#     f = open(f'{x}.x', 'r')
#     tool.append(f.read())
#     f.close()

while go_again == 'y':
    clear_screen()
    main_menu()
    go_again = input('Keep playing? (Y/N): ').lower()
 
Last edited:
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Check this trick
Python:
rck_ppr_scssr = ('', 'Rock', 'Paper', 'Scissor')

then you don't need this part of the code
Python:
user_selection -= 1

Full code: on-line
Python:
import os

def clear_screen():
    os.system('cls' if os.name in ('nt', 'dos') else 'clear')

def match(user_selection):
    print(f'You are {rck_ppr_scssr[user_selection]}')
    print(rck_ppr_scssr[user_selection].upper())

def main_menu():
    while True:
        try:
            clear_screen()
            print(' '.join(rck_ppr_scssr))
            print('\nEnter your selection:')
            print('\n'.join([f'{i}. {rck_ppr_scssr[i]}.' for i in [1, 2, 3]]))
    
            user_selection = int(input(': '))
            if not user_selection in [1, 2, 3]:
                clear_screen()
                print('Select a number between 1 and 3')
                input('Press ENTER to continue ')
                continue
            break
        except:
            clear_screen()
            print('Please enter only digits')
            input('Press ENTER to continue ')
            continue

    match(user_selection)


rck_ppr_scssr = ('', 'Rock', 'Paper', 'Scissor')

# for x in rck_ppr_scssr:
#     f = open(f'{x}.x', 'r')
#     tool.append(f.read())
#     f.close()

go_again = 'y'
while go_again in ['y', 'yes']:
    main_menu()
    go_again = input('Keep playing? (Y/N): ').lower()
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Check this trick
Python:
rck_ppr_scssr = ('', 'Rock', 'Paper', 'Scissor')

then you don't need this part of the code
Python:
user_selection -= 1

Full code: on-line
Python:
import os

def clear_screen():
    os.system('cls' if os.name in ('nt', 'dos') else 'clear')

def match(user_selection):
    print(f'You are {rck_ppr_scssr[user_selection]}')
    print(rck_ppr_scssr[user_selection].upper())

def main_menu():
    while True:
        try:
            clear_screen()
            print(' '.join(rck_ppr_scssr))
            print('\nEnter your selection:')
            print('\n'.join([f'{i}. {rck_ppr_scssr[i]}.' for i in [1, 2, 3]]))
   
            user_selection = int(input(': '))
            if not user_selection in [1, 2, 3]:
                clear_screen()
                print('Select a number between 1 and 3')
                input('Press ENTER to continue ')
                continue
            break
        except:
            clear_screen()
            print('Please enter only digits')
            input('Press ENTER to continue ')
            continue

    match(user_selection)


rck_ppr_scssr = ('', 'Rock', 'Paper', 'Scissor')

# for x in rck_ppr_scssr:
#     f = open(f'{x}.x', 'r')
#     tool.append(f.read())
#     f.close()

go_again = 'y'
while go_again in ['y', 'yes']:
    main_menu()
    go_again = input('Keep playing? (Y/N): ').lower()

I love this. So much to learn from forums. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top