[Tutor] Rock,Paper Scissors (was no subject)

Alan Gauld alan.gauld at yahoo.co.uk
Wed Feb 8 07:05:49 EST 2017


On 08/02/17 10:02, တာန္ခတ္သန္ wrote:
> Thanks for your answers. Your explanation makes me understand what I
> didn't understand and it discourages me to go on Python in other way
> as I feel like this doesn't suit to me. :'(

Don't be discouraged. Your central program was very nearly right and
your mistakes
are just the normal kinds of mistakes that beginners make so you are
basically
on the right track.

> Any way, can you please help and guide me to get done this. I have
> some questions:
>
> 1. Could I define player value to 2/3 values like player = 'r' 'p' 's'
> , or should  I set just any one of them.

I would just set one, the important thing is that you should always set
default
values to the same type as the real values. In this case that means a
string.
You could even use an empty string "" if you like.

> 2. For this command pc = random.choice(
> list(game_command.keys())[:3]), I don't really how to control. I don't
> want the computer picks 'e' as you said.
>     Can you please  help me how to write there. I have been running a
> hundred times and it doesn't pick 'e', so I thought it works.

Yes, that is the problem, most of the time these things will work but you
cannot guarantee it. In this case, because there are only 3 values,
you could just use the values directly:

pc = random.choice( ['r','p','s'])

If you really prefer a variable, and it is better practice, then you can
change your variable definitions:

weapons = ['r','p','s']

choices = weapons + ['e']
game_command = dict(zip(choices,['Rock','Paper','Scissors','Exit']))

then use

pc = random.choice( weapons )


> 3. Is there any problem if I didn't print the player score when PC won?

The specification asked for the score to be printed:
-------------------------------
For the game result the program must return details of the following:
- The user’s selection
- The computer’s selection
- The winning hand
- A running total of user wins including this game and previous games
------------------
It did not say to only do that if the player won...

> 4. Also how the exception to be generated.

Test the input value is valid and if not, use:

raise ValueError

The value is not valid if it is not in choices....

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



More information about the Tutor mailing list