I'm learning Python through an online course and there's this Rock Paper Scissors I've been writing through the course, but as I finish with the check_for_winning_throw part, four errors come out followed by "Process finished with exit code 1". I've tried rewriting the variables for every throw, but they are the same, checking every line but I can't find the error.
import random
rolls = {
'rock': {
'defeats': ['scissors'],
'defeated_by': ['paper']
},
'paper': {
'defeats': ['rock'],
'defeated_by': ['scissors'],
},
'scissors': {
'defeats': ['paper'],
'defeated_by': ['rock'],
},
}
def main():
show_header()
play_game("You", "Computer")
def show_header():
print("---------------------------")
print(" Rock Paper Scissors v2")
print(" Data Structure Edition ")
print("---------------------------")
def play_game(player_1, player_2):
rounds = 3
wins_p1 = 0
wins_p2 = 0
roll_names = list(rolls.keys())
while wins_p1 < rounds and wins_p2 < rounds:
roll1 = get_roll(player_1, roll_names)
roll2 = random.choice(roll_names)
if not roll1:
print("Try again!")
continue
print(f"{player_1} roll {roll1}")
print(f"{player_2} rolls {roll2}")
winner = check_for_winning_throw(player_1, player_2, roll1, roll2)
if winner is None:
print("This round was a tie!")
else:
print(f'{winner} takes the round!')
if winner == player_1:
wins_p1 += 1
elif winner == player_2:
wins_p2 += 1
print(f"Score is {player_1}: {wins_p1} and {player_2}: {wins_p2}.")
print()
if wins_p1 >= rounds:
overall_winner = player_1
else:
overall_winner = player_2
print(f"{overall_winner} wins the game!")
def check_for_winning_throw(player_1, player_2, roll1, roll2):
winner = None
if roll1 == roll2:
print("The play was tied!")
outcome = rolls.get(roll1, {})
if roll2 in outcome.get('defeats'):
return player_1
elif roll2 in outcome.get('defeated_by'):
return player_2
return winner
def get_roll(player_name, roll_names):
print("Available rolls:")
for index, r in enumerate(roll_names, start=1):
print(f"{index}. {r}")
text = input(f"{player_name}, what is your roll? ")
selected_index = int(text) - 1
if selected_index < 0 or selected_index >= len(rolls):
print(f"Sorry {player_name}, {text} is out of bounds!")
return None
return rolls[selected_index]
if __name__ == '__main__':
main()
This is the outcome:
---------------------------
Rock Paper Scissors v2
Data Structure Edition
---------------------------
Available rolls:
1. rock
2. paper
3. scissors
You, what is your roll?
Here I type "1" for Rock and then this comes out:
Traceback (most recent call last):
File "C:/Users/Bianconiglio/AppData/Local/Programs/Python/esercizi/notepad/rpsgame.py", line 101, in <module>
main()
File "C:/Users/Bianconiglio/AppData/Local/Programs/Python/esercizi/notepad/rpsgame.py", line 21, in main
play_game("You", "Computer")
File "C:/Users/Bianconiglio/AppData/Local/Programs/Python/esercizi/notepad/rpsgame.py", line 39, in play_game
roll1 = get_roll(player_1, roll_names)
File "C:/Users/Bianconiglio/AppData/Local/Programs/Python/esercizi/notepad/rpsgame.py", line 98, in get_roll
return rolls[selected_index]
KeyError: 0
Process finished with exit code 1
In the get_roll
function, you are returning rolls[selected_index]
but rolls
is a dictionnary with three keys (rock, paper, scissors), and selected_index
is 0, 1 or 2.
Change the return value to list(rolls.keys())[selected_index]
and you will be good !