[Tutor] (no subject)
တာန္ခတ္သန္
pkhattan at gmail.com
Tue Feb 7 16:09:32 EST 2017
Hi,
I am just start learning Python and I have an activity to do
RockPaperScissors game using dictionary. I have written my code and it
doesn't work at all. I am sending my code and the
requirements. Can you please have a look and point out what is wrong there.
Thank you very much.
Warm regards,
Pau
program requirements:
The program will display an appropriate welcome message and user
instructions.
The program must use a Python dictionary to hold data shown in the table
below:
Game Command Value
R Rock
P Paper
S Scissors
E Exit
The player will enter their choice of shape by entering a valid Game
Command.
Any invalid input must generate an exception that is handled and used to
display an appropriate error message.
For correct inputs, the computer will randomly pick a dictionary item
(other than ‘Exit’) and compare that value with the user’s, before
returning the game result.
For the game result the program must return details of the following:
o The user’s selection
o The computer’s selection
o The winning hand
o A running total of user wins including this game and previous games
The program will repeat the game until the user exits by entering ‘e’.
# RockPaperScissors
import random
print ("Hello.. Welcome from Rock, Paper, Scissors Game!!\n"
+"Game instruction:\n"
+"You will be playing with the computer.\n"
+"You need to choose one of your choice: Rock, Paper or Scissors.\n"
+"Enter 'e' to exit the game.\n")
game_command = {"r":"Rock","p":"Paper","s":"Scissors","e":"Exit"}
score = 0
player = 0
try:
while player == 'r' and player == 'p' and player == 's':
pc = random.choice( list(game_command.keys())[:3])
player = input("\nPlease enter your choice:'r','p' or 's': ")
print ("You selects: ", player)
print ("PC selects:",pc)
if player == pc:
print("It's a Tie!")
score = score
print("Your score is: ",score)
elif player == "r":
if pc == "p":
print("PC win!")
else:
print("You win!")
score = score + 1
print("Your score is: ",score)
elif player == "p":
if pc == "s":
print("PC win!")
else:
print("You win!")
score = score + 1
print("Your score is: ",score)
elif player == "s":
if pc == "r":
print("PC win!")
else:
print("You win!")
score = score + 1
print("Your score is: ",score)
elif player == 'e' :
break
print("You exit the game.")
except ValueError:
print("\nInvalid character,try again!")
More information about the Tutor
mailing list