Skip to content

Instantly share code, notes, and snippets.

@malep2007
Last active November 29, 2018 09:49
Show Gist options
  • Save malep2007/09b73456de0f6d0d00f04ac0621b7129 to your computer and use it in GitHub Desktop.
Save malep2007/09b73456de0f6d0d00f04ac0621b7129 to your computer and use it in GitHub Desktop.
def validate(hand):
if hand < 0 or hand > 2:
return False
return True
def print_hand(hand, name='Guest'):
hands = ['Rock', 'Paper', 'Scissors']
print(name + ' picked: ' + hands[hand])
# Define the judge function
def judge(player,computer):
# Add control flow based on the comparison of player and computer
if player==computer:
return 'Draw'
# used wrong else if statement
elif (player==0 and computer==1) or (player==1 and computer==2) or (player==2 and computer==0):
return 'Lose'
# Indentation of else was also off
else:
return 'Win'
print('Starting the Rock Paper Scissors game!')
player_name = input('Please enter your name: ')
print('Pick a hand: (0: Rock, 1: Paper, 2: Scissors)')
player_hand = int(input('Please enter a number (0-2): '))
if validate(player_hand):
computer_hand = 1
print_hand(player_hand, player_name)
print_hand(computer_hand, 'Computer')
# Assign the return value of judge to the result variable
result=judge(player_hand,computer_hand)
# Print the result variable
print('Result: ' + result)
else:
print('Please enter a valid number')
@malep2007
Copy link
Author

So this is the code you sent me. Let's break this down bit by bit.

  • Line 16 has a wrong else if statement. In python we use elif instead. So should have it as
     elif (player==0 and computer==1) or (player==1 and computer==2) or (player==2 and computer==0):
  • the other problem is on line 18; the indentation is not correct, it should be at the same indentation level as elif in line 16.

So looking at your code, it should be like this at the end.

def validate(hand):
    if hand < 0 or hand > 2:
        return False
    return True

def print_hand(hand, name='Guest'):
    hands = ['Rock', 'Paper', 'Scissors']
    print(name + ' picked: ' + hands[hand])

# Define the judge function
def judge(player,computer):
    # Add control flow based on the comparison of player and computer
   if player==computer:
       return 'Draw'
   elif (player==0 and computer==1) or (player==1 and computer==2) or (player==2 and computer==0):
       return 'Lose'
   else:
        return 'Win'


print('Starting the Rock Paper Scissors game!')
player_name = input('Please enter your name: ')

print('Pick a hand: (0: Rock, 1: Paper, 2: Scissors)')
player_hand = int(input('Please enter a number (0-2): '))

if validate(player_hand):
    computer_hand = 1

    print_hand(player_hand, player_name)
    print_hand(computer_hand, 'Computer')

    # Assign the return value of judge to the result variable
    result=judge(player_hand,computer_hand)
    # Print the result variable
    print('Result: ' + result)
else:
    print('Please enter a valid number')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment