On 07/02/17 21:09, တာန္ခတ္သန္ wrote:

> RockPaperScissors game using dictionary. I have written my code and it
> doesn't work at all.

So what does it do? Crash your PC? Give an error? Run silently and stop?
You need to be precise when telling us these things. If you get an error
message send us the full text.

> requirements. Can you please have a look and point out what is wrong there.

> 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

zero is not a valid value for player. Your code expects
a character not a number.

> try:
>     while player == 'r' and player == 'p' and player == 's':

'and' in computing implies both things must be true at the same time.
player can only have one value so this test will always be false and
your loop will never run. I suspect you either want to substitute 'or'
for 'and', or use:

while player in ('r','s','p'): ...

But since you use break to exit the loop later you could
just use:

while True: ...

To force the loop to run.
This is probably the most common technique in Python
for this kind of scenario.

>         pc = random.choice( list(game_command.keys())[:3])

Note that Python does not guarantee that a dictionary's values
are returned in the order you created them. So this could return
a different set of values from what you expect, eg ['r','e','p']

>         player = input("\nPlease enter your choice:'r','p' or 's': ")

shouldn't you do this before starting your loop too?
Just to be sure the player wants to play? And shouldn't the
player be allowed to choose 'e' too?
Otherwise how do they know how to exit?

>         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!")

shouldn't you print the score here too?

>             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!")

You never raise a ValueError so when would this
exception be generated?

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to