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: