Python Forum
Rock Paper Scissors with dictionaries - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Rock Paper Scissors with dictionaries (/thread-26430.html)



Rock Paper Scissors with dictionaries - ewgreht - May-01-2020

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()



RE: Rock Paper Scissors with dictionaries - pyzyx3qwerty - May-01-2020

You can only have one entry for each key in a dictionary


RE: Rock Paper Scissors with dictionaries - deanhystad - May-01-2020

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!