-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrock_paper_scissors.py
More file actions
91 lines (72 loc) · 2.87 KB
/
rock_paper_scissors.py
File metadata and controls
91 lines (72 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
###############################################################################
# Description: Rock, paper, and scissor game against the computer with random choices
###############################################################################
import random
# get computer's choice
def get_computer_choice():
random_choice = random.randint(1,3)
if random_choice == 1:
return 'rock'
elif random_choice == 2:
return 'paper'
else:
return 'scissors'
# get player's choice
def get_player_choice():
choice = input('Choose rock, paper, or scissors: ')
while choice != 'rock' and choice != 'paper' and choice != 'scissors':
print('You made an invalid choice. Please try again.')
choice = input('Choose rock, paper, or scissors: ')
if choice == 'rock' or choice == 'paper' or choice == 'scissors':
return choice
def main():
# Write your mainline logic here ------------------------------------------
user_choice = get_player_choice()
comp_choice = get_computer_choice()
print(f' The computer chose {comp_choice}, and you chose {user_choice}.')
winner = get_winner(comp_choice, user_choice)
while winner == 'tie':
print(' Its a tie. Starting over.')
print('')
user_choice = get_player_choice()
comp_choice = get_computer_choice()
print(f' The computer chose {comp_choice}, and you chose {user_choice}.')
winner = get_winner(comp_choice, user_choice)
break
if winner == 'computer':
print(f' {comp_choice} beats {user_choice}')
print(' You lost. Better luck next time.')
elif winner == 'player':
print(f' {user_choice} beats {comp_choice}')
print(' You won the game!')
print('Thanks for playing.')
# get winner function
def get_winner(comp_choice, user_choice):
#paper_message = ' paper beats rock'
#rock_message = ' rock beats scissors'
#scissors_message = ' scissors beats paper'
winner = ''
if comp_choice == user_choice:
winner = 'tie'
elif comp_choice == 'rock' and user_choice == 'scissors':
#print(rock_message)
winner = 'computer'
elif comp_choice == 'scissors' and user_choice == 'rock':
#print(rock_message)
winner = 'player'
elif comp_choice == 'scissors' and user_choice == 'paper':
#print(scissors_message)
winner = 'computer'
elif comp_choice == 'paper' and user_choice == 'scissors':
#print(scissors_message)
winner = 'player'
elif comp_choice == 'paper' and user_choice == 'rock':
#print(paper_message)
winner = 'computer'
elif comp_choice == 'rock' and user_choice == 'paper':
#print(paper_message)
winner = 'player'
return winner
# Don't change this -----------------------------------------------------------
if __name__ == '__main__':
main()