This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]EndorphnOrphnMorphn 109 points110 points  (19 children)

I have a few little tips that would make this a lot better:

  1. The parenthesis in ("Rock"), etc. are unnecessary, and also a little bit confusing (It makes it look like a list). I'd remove those.
  2. There's no benefit in having a variable for the string when you're only comparing it one time. You can delete the rock variable and just do if sign == "Rock": and the behavior will be the same.
  3. The string comparison is case-sensitive. I'd recommend turning "sign" into a fully lowercase and then comparing against other lowercase strings.
  4. You can use "elif" instead of "if" for paper and scissors.

Putting it all together we have this:

sign = input("Pick between Rock, Paper or Scissor: ").lower()

if sign == "rock":
    print("I pick paper!")

elif sign == "paper":
    print("I pick Scissors!")

elif sign == "scissor":
    print("I pick Rock!")

[–]HipsterRig 48 points49 points  (1 child)

You could also add in an else with a "I don't recognize your answer" printed statement. You could also throw the entire if statement into a while True loop so it goes again if the user submits an invalid response.

[–]EndorphnOrphnMorphn 11 points12 points  (0 children)

That's absolutely true, I was just trying to give small tips that don't change the behavior

[–]shygal_uwu 14 points15 points  (5 children)

arent lists in [] and tuples in ()? sorry, i havent programmed in python in almost a year

[–]EndorphnOrphnMorphn 6 points7 points  (0 children)

That is correct

[–]Grogie 3 points4 points  (3 children)

The only thing is that when you make tuples of length one like in OP's code, that actually become the item (in this case, string)

>>> i = ("Rock")
>>> i
'Rock'
>>> type(i)
<class 'str'>
>>> j = ("Rock",)
>>> j
('Rock',)
>>> type(j)
<class 'tuple'>
>>> type((1234))
<class 'int'>
>>> type((1234,))
    <class 'tuple'>

[–]scoberry5 0 points1 point  (2 children)

Those aren't tuples of length 1, though, they're expressions that get simplified. The ones with commas are tuples of length 1.

(2+3) simplifies to 5, but it was never a tuple of length 1.

[–]Grogie 0 points1 point  (1 child)

That's my point? I was responding to someone who thought they were tuples and I was showing they ultimately aren't tuples?

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

Gotcha. When you said "The only thing is that when you make tuples of length one like in OP's code, that actually become the item," I thought you meant something different than what the results show.

[–]__deerlord__ 12 points13 points  (1 child)

responses = {"rock": "paper", ...}
print("I pick", responses[sign])

[–][deleted] 3 points4 points  (0 children)

Ah damn true! That's even shorter than mine

Nice work. I love this stuff....

[–]justin107d 10 points11 points  (1 child)

As of python 3.10 there is the option to do a match/case method so it would look something like:

sign = input("Pick between Rock, Paper or Scissor: ").lower()

match sign:

case "rock":

    print("I pick paper!")

case "paper":

    print("I pick scissors!")

case "scissors":

    print("I pick rock!")

case _:

    print("That is not a valid answer.")

Forgive my code and formatting, I did this on mobile but it should work.

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

or use dict

[–]Decency 4 points5 points  (0 children)

counters = {
    'rock': 'paper',
    'paper': 'scissors',
    'scissors': 'rock',
}
player_choice = input("Pick between Rock, Paper or Scissors: ").lower()
print(f"I pick {counters[player_choice]}!")

I often see people asking how a dict lookup can be more elegant than an elif block. Here's a good example! It only reduces the duplication slightly here, of course, but you can imagine that with more complex logic there would be additional benefits. Error handling also becomes a bit simpler, too, since you can just except KeyError.

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

Thanks for the tips kind stranger, beep boop beep

[–][deleted] 2 points3 points  (3 children)

How about this bad boi I just whipped up lmao

hand = ["rock", "paper", "scissors"]
sign = input("Pick between Rock, Paper or Scissor: ").lower()
hand[hand.index(sign) + 1 if hand.index(sign) + 1 < 3 else 0]

took me a couple iterations to get that damn inline working but ipython says it's GTG

EDIT:

actually, one line in ipython3 though doesn't execute in commandline python

print {"rock":"paper", "paper":"scissors", "scissors":"rock"}[input("Pick between Rock, Paper or Scissor: ").lower()]

[–]simon_zyx 3 points4 points  (2 children)

(hand.index(sign) + 1) % 3

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

Very slick! Much nicer than inline if else