Can't fix error in my code

Problem description:
Traceback (most recent call last):
File “main.py”, line 35, in
check_win(choices[‘player’], choices[‘computer’])
KeyError: ‘player’

Expected behavior:
it should run rock paper scissors game

Actual behavior:

Steps to reproduce:

Bug appears at this link:

Browser/OS/Device:
vivaldi/windows10 64 bit/ pc

Can you give your repl link? Also this is not a replit bug. It is a bug in your code so it should probably be categorized as #code-help

1 Like

Sorry, it is my first time using replit. Ofc, here is the link : https://replit.com/@KostaDamjanovi1/Basic-Python-freecodecamp#main.py

You add the key as "player:" notice the colon (":") try this:

import random


def get_choices():
  player_choice = input("Enter a choice (rock, paper or scissors): ")
  options = ["rock", "paper", "scissors"]
  computer_choice = random.choice(options)
  choices = {"player": player_choice, "computer:": computer_choice} # the code is the same but I just deleted ":" in the player entry
  return choices


def check_win(player, computer):
  print(f"You chose {player}, computer chose {computer}")
  if player == computer:
    return "It's a tie!"
  elif player == "rock":
    if computer == "scissors":
      return "Rock smashes scissors! You win!"
    else:
      return "Paper covers rock! You lose."
  elif player == "paper":
    if computer == "rock":
      return "Paper covers rock! You win!"
    else:
      return "Scissors cut paper! You lose."
  elif player == "scissors":
    if computer == "paper":
      return "Scissors cut paper! You win!"
    else:
      return "Rock smashes scissors! You lose."


choices = get_choices()
print(choices)
check_win(choices['player'], choices['computer'])
print(check_win())

2 Likes

Heeey, thank you very much!!! Didn’t code in a while so you just replied in time! I was almost there to leave studying again :tada: thanks for real! :heart:

2 Likes

your welcome :smiley: have fun :tada:

2 Likes