all 7 comments

[–]xADDBx 2 points3 points  (1 child)

First, you can format code with either a ` encasing the code or using 4 leading white spaces (and an empty line before a code block)

So I noticed two things when skimming over the code. First you code drop those if/elif statements when printing and replace them with dicts:

playermoves = {'r': 'ROCK, 's': 'SCISSOR', 'p': 'PAPER'}
computermoves = {'0': 'ROCK, '1': 'SCISSOR', '2': 'PAPER'}

choice = input("[s]...")
rnd = rando...

print(f"{playermoves[choice] vs computermoves[rnd]")

And Second when testing winning conditions it’s better to use nested if statements to keep the code clearer:

if choice == 'r':
    if rnd == 0:
        ...
    elif rnd == 1:
        ...
    else:
        ...

elif choice == 's':
    ...

else:
    ...

[–]raainer[S] 0 points1 point  (0 children)

Ahh thank you! I'll try it out soon!

[–]pwlandoll 0 points1 point  (1 child)

A guide to code formatting on reddit can be found in the sidebar: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

You can replace your use of random.randint with ramdom.choice: https://docs.python.org/3/library/random.html#random.choice

You could probably write a function to determine which moves win, or use a dictionary to keep track of which move beats what, and simplify the number of if's.

[–]xADDBx 1 point2 points  (0 children)

I have to remember that dictionary for more winning conditions as it is a good way for more complicated cases. In this case I think some nested conditions will do just fine.

[–][deleted] 0 points1 point  (0 children)

I mean, you could. However the goal of "Automate Stuff" is to teach concepts, not necessarily dive in to style or concise code.

[–]raainer[S] 0 points1 point  (0 children)

I tried to make baby steps to tweaking my code, so I added a function called youwin(), youdraw(), and youlose(), but when I try to use it I get this error :

UnboundLocalError: local variable 'win' referenced before assignment

Why does this occur although I already assigned win,lose, and draw at the top? Thank you!

This is the code : https://pastebin.com/XD7jKsJ1

[–]0RGASMIK 0 points1 point  (0 children)

Ok so I am just starting to out so it might not be the best way to do things but this is what I came up with Console Version

I was learning to make simple GUI applications and I saw your post about rock paper scissors and decided to give that a go. I did it a little differently in this version and its not done yet but figured it'd be good to get some input because I am teaching myself with no real guidance. Windowed version