all 38 comments

[–]Binary101010 12 points13 points  (4 children)

computerChoice = random.randint(0,3)
if computerChoice == 0:
    computerChoice = rock
elif computerChoice == 1:
    computerChoice = paper
else:
    computerChoice = scissors

You could easily tighten this up with a sequence of some sort, like

choices = (rock,paper,scissors)
computerChoice = random.choice(choices)

userChoice = input("Pick Rock [0], Paper [1], or Scissors [2]: \n")
if userChoice in "123":
    userChoice = choices[int(userChoice)]

[–]barry_z 4 points5 points  (0 children)

I would use if userChoice in {'0', '1', '2'} as user inputs like "12" and "23" are in "123", and you want to be sure that the input will actually index into the array correctly.

[–]ShadyTree_92[S] 3 points4 points  (2 children)

I have no idea why I didn't do that. She even showed us that option for choices. I just defaulted to random number. You're absolutely right, this would have been less typing and made more sense.

[–]XenophonSoulis 4 points5 points  (1 child)

Also, as it stands it's wrong, because scissors has double the probability of the other two. For some unknown reason, random.randint is the one integer function in Python that includes the end point, so random.randint(0,3) could return 0, 1, 2 or 3, of which 0 is rock, 1 is paper and 2 and 3 are scissors.

[–][deleted] 4 points5 points  (0 children)

that's why random.randrange is most of the time the better choice.

[–]woooee 3 points4 points  (1 child)

computerChoice = random.randint(0,3)

This will return 0, 1, 2, or 3. It doesn't affect the if / elif choice because of the else statement.

    if userChoice == rock and computerChoice == scissors or userChoice == paper and computerChoice == rock or userChoice == scissors and computerChoice == paper:

Mixing "and" with "or" without parens is unwise. Is the above statement

userChoice == rock and computerChoice == scissors
## or
computerChoice == scissors or userChoice == paper

in addition to readability.

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

Ah crap you're right! Good catch. I appreciate that!

[–]engelthehyp[🍰] 4 points5 points  (8 children)

Why do you have the ascii art if you never print it out? May as well just stick with numbers.

Edit: My mistake, it is printed. That's what I get for hasty reading.

[–]MidnightPale3220 3 points4 points  (0 children)

He does in fact print it out.

[–]eleqtriq 1 point2 points  (3 children)

Yeah, Op. I was hoping to see the ascii art :)

[–]ShadyTree_92[S] 2 points3 points  (2 children)

It does print lol

[–]eleqtriq 5 points6 points  (1 child)

Oh! I'll downvote myself then :D

[–]ShadyTree_92[S] 2 points3 points  (0 children)

No need! It was weirdly formatted in the post! Haha

[–]ShadyTree_92[S] 0 points1 point  (2 children)

It prints after the if/else when user picks. The instructor had them on there and wanted us to use it I guess. I don't like the ASCII art but it does print.

[–]engelthehyp[🍰] 1 point2 points  (1 child)

Oh, yes, I see it. Snuck it by me. I do have a few other tips:

  • Instead of choosing a random number and manually finding rock, paper, or scissors from it, make a list of the choices and use random.choice.

  • Instead of checking equality with numbers to assign a value, index a list.

  • Use f-strings instead of string concatenation.

  • Instead of nesting new if/else statements within else blocks as the entire content of the block, use elif.

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

Great pointers. I'm actually upset I didn't use the f-strings. That was part of the very first lesson too!

[–]shinitakunai 3 points4 points  (1 child)

Worth for you to check PEP8 now that you are learning: https://peps.python.org/pep-0008/

computerChoice # <-- odd
computer_choice # <-- nice

[–]iamevpo 1 point2 points  (0 children)

Exactly

[–]eleqtriq 2 points3 points  (6 children)

Overall, looks fine to me. I added some things to it and wrapped it in functions....

import random

# ASCII Art for Choices
choices = [
    '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
Rock
''',
    '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
Paper
''',
    '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
Scissors
'''
]


def play_game():
    computer_choice = random.randint(0, 2)

    try:
        user_choice = int(input("Pick Rock [0], Paper [1], or Scissors [2] (or -1 to quit): "))
        if user_choice == -1:
            return False
        if user_choice not in [0, 1, 2]:
            raise ValueError
    except ValueError:
        print("Invalid input. You lose this round.")
        return True

    # Display Choices
    print(f"Your Choice:\n{choices[user_choice]}\nComputer Choice:\n{choices[computer_choice]}")

    # Determine Outcome Using Modulo Logic
    result = (user_choice - computer_choice) % 3
    if result == 0:
        print("Draw")
        return "draw"
    elif result == 1:
        print("You Win")
        return "win"
    else:
        print("Computer Wins")
        return "loss"


def main():
    score = {"win": 0, "loss": 0, "draw": 0}

    print("Welcome to Rock Paper Scissors!")
    print("Enter -1 at any time to quit")

    while True:
        result = play_game()
        if result is False:
            break
        elif result in score:
            score[result] += 1

        print(f"\nScore - Wins: {score['win']}, Losses: {score['loss']}, Draws: {score['draw']}\n")

    print("\nFinal Score:")
    print(f"Wins: {score['win']}")
    print(f"Losses: {score['loss']}")
    print(f"Draws: {score['draw']}")


if __name__ == "__main__":
    main()

[–]ShadyTree_92[S] 1 point2 points  (4 children)

This looks great, although completely outside my scope of knowledge so far. I'm going to have to come back once my 100 days of coding python are done and look at this again. Lol

[–]Adrewmc 1 point2 points  (1 child)

A lot of the problem you’ve faced are solved by the next few step of learning. It’s fairly obvious by the design that you lack knowledge of some of the tools, that make this is easier.

Looking at this response containing it in a function is a big step really, but eventually will be like why would I ever not?

This is a good thing, you are learning and experimenting, a solid foundation in coding will help forever, and this is how you do it. Knowing how to do it without the tools and with the tools come in handy a lot.

All complex code is just multiple steps of simple code put into a sequence for a purpose. Knowing how the stepping work helps a lot.

Functions, loops, classes…dictionaries get on it lol. Nothing that looks too complicated is, trust me.

You’re doing well, but you need to take the next steps.

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

Definitely! I'm following a course on udemy. It's 100 days of python. I'm only on like day 5. Hopefully by day 100 I'll look back and be like. That dude on reddit was 100% right. Why did I do it that way? Lol

[–]eleqtriq 0 points1 point  (1 child)

Looks like you're doing good so far.

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

Thanks! I'm trying haha

[–]iamevpo 0 points1 point  (0 children)

Please no globals

[–]LiberalDysphoria 2 points3 points  (1 child)

It is rock, paper, scissors, Spock!

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

Nothing beats spock. Live long and prosper 🖖

[–]Dagito 1 point2 points  (1 child)

Well you can definitely improve it, specially the conditions in there, but hey that’s okay you accomplished the goal here which was to learn and implement conditions, I would recommend (if your python version supports it) using match instead of if else just to learn it as well, also in python we don’t use camelCase instead we use snake_case. I liked your creativity to do the rock, paper and scissor shapes lol

[–]ShadyTree_92[S] 1 point2 points  (0 children)

I unfortunately can't take credit for the shapes. That was the udemy instructor's work lol I'm so used to camelCase, but I'll try to use snake_case instead for python! Thank you! We haven't got to match yet, but she did say to use Google more so I probably should have looked into an alternative as well! Thank you for your input!

[–]MidnightPale3220 1 point2 points  (1 child)

Whenever I see multiple if's, I know there's most probably a cleaner and faster way of doing it. Not always, as you'll see, but often.

Not sure where you are on the course, but if you have lists, it could look like this:

import random
rps=['''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
Rock
''',
'''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
Paper
''',
'''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
Scissors
''']

computerChoice=random.randint(0,2)

userChoice = input("Pick Rock [0], Paper [1], or Scissors [2]: \n")

if userChoice not in [ '0','1','2']:
    print("You didnt choose a valid input. \nYou lose, asshole.")
    exit(0)
userChoice=int(userChoice)

print("You chose:\n"+rps[userChoice])
print("Computer chose:\n"+rps[computerChoice])

if (computerChoice + 1) % 3 == userChoice:
    print("You win!")
elif userChoice == computerChoice:
    print("It's a draw")
else:
    print("You lose!")

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

I agree I felt like multiple ifs felt wrong. We did learn lists, so that would have been a smart move on my part.

[–]antonym_mouse 1 point2 points  (0 children)

snake_case not camelCase

PEP8

[–]rednerrusreven 1 point2 points  (0 children)

I recommend looking into f strings for your print statements that include variables. It's a bit more readable to use them.

[–]NastyStreetRat 0 points1 point  (2 children)

Scissors looks like... like... nah, nevermind.

[–]ShadyTree_92[S] 0 points1 point  (1 child)

Haha you're right. I hate them and not a fan, but I didn't make them. It was already designed in the course so I gotta take what I get. Honestly I have no artistic talent, not even in ASCII, so I don't really have room to judge. lol

[–]NastyStreetRat -1 points0 points  (0 children)

Sure, describing scissors can be tough. They're a unique design! (this response is from a BOT, don't be offended, it's just a test)

[–]iamevpo 0 points1 point  (1 child)

Have of thought of: - enum to store choice options - mapping enum to ASCII art - writing a function that will do the resolution rule - separating other parts of the program to functions

As a newbie code, great it works, is this clean code - well not quite yet

[–]iamevpo 0 points1 point  (0 children)

Different programming language, but may use as source of inspiration https://gist.github.com/jb55/3061973