Python Forum
Rock Paper Scissors with dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock Paper Scissors with dictionaries
#1
I am making a more advanced version of Rock Paper Scissors and I am using dictionaries to define my rules but I most of the time when I run the code I get an error message that I coded to only happen when the user types something in wrong but I get it even if I typed it correctly.
import time
import random

def game():
    print('Get ready!')
    computer_choice = ['rock','paper','scissors','witch','lizard']
    beats = {'scissors':'paper','paper':'rock','rock':'lizard','lizard':'witch','witch':'scissors','scissors':'lizard','lizard':'paper','paper':'witch','witch':'rock','rock':'scissors'}
    verbs = {'scissors':'cuts','paper':'suffocates','rock':'crushes','witch':'magics','lizard':'eats'}
    time.sleep(1)
    print('Rock')
    time.sleep(0.75)
    print('Paper')
    time.sleep(0.75)
    print('Scissors')
    time.sleep(0.75)
    print('Shoot')
    player_choice = input('(Type Rock, Paper, Scissors, Witch or Lizard and then press enter)').lower()
    computer_choice = random.choice(computer_choice)
    time.sleep(1)
    if computer_choice == player_choice:
        print('Computer chose ' + computer_choice + '. You tie!')
    elif beats[player_choice] == computer_choice:
        print(player_choice + ' ' + verbs[player_choice] + ' ' + computer_choice)
        print('Computer chose ' + computer_choice + '. You win!')
    elif beats[computer_choice] == player_choice:
        print('Computer chose ' + computer_choice + '. You lose!')
        print(computer_choice + ' ' + verbs[computer_choice] + ' ' + player_choice)
    else:
        print('error')
    play_again = input('Press enter to play again or X to end the game')
    if play_again == "":
        game()
    else:
        print('Thank you for playing')

game()
Reply
#2
You can only have one entry for each key in a dictionary
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
A dictionary can only have one entry for each key. Your 'beats' dictionary says lizard beats scissors because that was the last scissors entry added to the beats dictionary. Poor rock, you have lost your ability to crush scissors!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock Paper Scissors Project in Python ankitdixit 8 4,816 Feb-23-2024, 03:14 PM
Last Post: DPaul
  Trying to create a visual rock paper scissors game urmom33 1 1,027 Dec-03-2022, 09:12 PM
Last Post: deanhystad
  Rock paper scissors in python with "algorithm" Agat0 23 5,995 Mar-01-2022, 03:20 PM
Last Post: Agat0
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,590 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Odd behavior with Rock Paper Scissor game DustinKlent 2 1,932 Aug-27-2020, 03:55 PM
Last Post: DustinKlent
  Trying to implement Best of 3 logic in rock paper scissors game ShAhCh 5 3,349 May-11-2020, 04:31 PM
Last Post: ShAhCh
  Rock, Paper, Scissors.. Help..hidden bug xxunknownxx 4 2,661 Mar-19-2020, 02:46 AM
Last Post: jefsummers
  Problem with Basic Rock Paper Scissors code BirinderSingh 3 2,437 Sep-13-2019, 03:28 PM
Last Post: ichabod801
  Rock, Paper, Scissors Help jyou99 1 2,444 Mar-26-2018, 04:07 PM
Last Post: nilamo
  Rock Paper Scissors (Need help) Lukili 1 2,447 Jan-14-2018, 10:00 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020