Python Forum
Roshambo with only 1 if switch
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Roshambo with only 1 if switch
#11
Just thinking aloud: I need to know only winner, because logically another is looser. There are only three winning combinations (below, list of tuples, winner is first looser is second), all other combinations are either loosing or draw.

>>> first_wins = [('Rock', 'Scissors'), ('Paper', 'Rock'), ('Scissors', 'Paper')]
Comparison to determine winner could therefore be:

>>> if user_choice == computer_choice:
...     # draw
... elif (user_choice, computers_choice) in first_wins:
...     # user wins
... else:
...     # computer wins


One idea to improve clarity is to move user input validation into separate function. Something like this:

def validate_input(question):
    """Validate input to the question."""
    allowed = ['paper', 'scissors', 'rock', 'quit']
    while True:
        answer = input(question).lower()
        if answer in allowed:
            return answer
        else:
            print((f'Expected one from: {", ".join(allowed)} ' 
                   f'but your input was {answer}'))

user_choice = validate_input('Enter your choice, one from: paper, scissors, rock, quit ')
This enables to separate what is done from how it is done and code is (subjectively) more readable:

while True:
    user_choice = validate_input('Enter your choice, one from: paper, scissors, rock, quit ')
    if user_choice == 'quit':
        break
    computer_choice = random.choice([pair[0] for pair in first_wins])
    if user_choice == computer_choice:
        # draw
    elif (user_choice, computer_choice) in first_wins:
        # user wins
    else:
        # computer wins
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#12
(Jan-27-2019, 09:02 AM)perfringo Wrote: Just thinking aloud: I need to know only winner, because logically another is looser[sic]. There are only three winning combinations (below, list of tuples, winner is first looser[sic] is second), all other combinations are either loosing[sic] or draw.

>>> first_wins = [('Rock', 'Scissors'), ('Paper', 'Rock'), ('Scissors', 'Paper')]

You lost me here. I liked your original %3 concept for projects of higher complexity, but If you're going to repeat the moves I like the readability of my original compound dictionary much better. However, the move validation in a function is a good idea to improve readability.
Reply
#13
(Jan-27-2019, 10:19 PM)Clunk_Head Wrote: You lost me here.

My first post suggested an alternative approach. Last one was about trying to make your original code less verbose and therefore less error prone. If somebody (in future) discovers this thread and considering %3 solution too complicated/foreign he/she can use it to write more DRY (Don't Repeat Yourself) version of your original code.

After all, this is discussion forum where different possibilities are discussed Smile
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  switch to python3 Skaperen 0 2,117 Jul-03-2018, 12:55 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020