-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompleted_assign1.py
143 lines (120 loc) · 4.28 KB
/
completed_assign1.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# CodeSkulptor runs Python programs in your browser.
# Click the upper left button to run this simple demo.
# CodeSkulptor runs in Chrome 18+, Firefox 11+, and Safari 6+.
# Some features may work in other browsers, but do not expect
# full functionality. It does NOT run in Internet Explorer.
# Rock-paper-scissors-lizard-Spock template
# The key idea of this program is to equate the strings
# "rock", "paper", "scissors", "lizard", "Spock" to numbers
# as follows:
#
# 0 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors
# helper functions
import random
def name_to_number(name):
# delete the following pass statement and fill in your code below
if name.lower() == 'rock':
return 0
elif name.lower() == 'spock':
return 1
elif name.lower() == 'paper':
return 2
elif name.lower() == 'lizard':
return 3
elif name.lower() == 'scissors':
return 4
else:
print("Sorry, that is not one of the choices")
# convert name to number using if/elif/else
# don't forget to return the result!
def number_to_name(number):
# delete the following pass statement and fill in your code below
if number == 0:
return "rock"
elif number == 1:
return "Spock"
elif number == 2:
return "paper"
elif number == 3:
return "lizard"
elif number == 4:
return "scissors"
else:
print("Sorry, pick a number between 0 to 4")
# convert number to a name using if/elif/else
# don't forget to return the result!
def rpsls(player_choice):
# print a blank line to separate consecutive games
print("\n")
# print out the message for the player's choice
print("Player chooses "+player_choice)
# convert the player's choice to player_number using the function name_to_number()
player_number = name_to_number(player_choice)
# compute random guess for comp_number using random.randrange()
comp_number = random.randrange(0,4)
# convert comp_number to comp_choice using the function number_to_name()
comp_choice = number_to_name(comp_number)
# print out the message for computer's choice
print("Computer chooses "+comp_choice)
# compute difference of comp_number and player_number modulo five
answer = (comp_number - player_number)%5
#I ran another version of the program, with random numbers for both
#player and computer. I used player numbers, and assigned outcomes
#based on what the computer drew to get the remainder, and deducting
#from the rules
# use if/elif/else to determine winner, print winner message
#I realize it looks messy and could be refactored, but this is so I can
#debug the if statements more easily.
if(answer == 1):
if(player_number == 1):
print("Computer wins!")
elif(player_number == 3) :
print("Player wins!")
elif(player_number == 4):
print("Computer wins")
elif(player_number == 2):
print("Computer wins!")
else:
print("Computer wins!")
elif(answer == 2):
if((player_number == 1) or (player_number == 3)):
print("Computer wins!")
elif(player_number == 2):
print("Computer wins!")
elif(player_number == 4):
print("Computer wins!")
else:
print("Computer wins!")
elif(answer == 3):
if((player_number == 0) or (player_number == 3)):
print("Player wins!")
elif(player_number == 1):
print("Computer wins!")
elif(player_number == 2):
print("Player wins!")
else:
print("Player wins!")
elif(answer == 4):
if(player_number == 0):
print("Computer wins!")
elif(player_number == 1):
print("Player wins!")
elif(player_number == 4):
print("Player wins!")
elif(player_number == 2):
print("Player wins!")
else:
print ("Player wins!")
else:
print("Player and computer tie!")
# test your code - THESE CALLS MUST BE PRESENT IN YOUR SUBMITTED CODE
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
# always remember to check your completed program against the grading rubric