FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/rpsls_game.py at master · moehuster/python · GitHub
moehuster
/
python
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
rpsls_game.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (69 loc) · 2.22 KB
Breadcrumbs
python
/
rpsls_game.py
Copy path
File metadata and controls
83 lines (69 loc) · 2.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# 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
import
random
# helper functions
def
name_to_number
(
name
):
# convert name to number using if/elif/else
if
(
name
==
"rock"
):
return
0
elif
(
name
==
"Spock"
):
return
1
elif
(
name
==
"paper"
):
return
2
elif
(
name
==
"lizard"
):
return
3
elif
(
name
==
"scissors"
):
return
4
else
:
return
-
1
def
number_to_name
(
number
):
# convert number to a name using if/elif/else
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
:
return
"none"
def
rpsls
(
player_choice
):
# print a blank line to separate consecutive games
print
""
# 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_name
=
number_to_name
(
comp_number
)
# print out the message for computer's choice
print
"Computer chooses"
,
comp_choice_name
# compute difference of comp_number and player_number modulo five
# use if/elif/else to determine winner, print winner message
ret
=
(
player_number
-
comp_number
)
%
5
if
(
ret
==
3
or
ret
==
4
):
print
"Computer wins!"
elif
(
ret
==
1
or
ret
==
2
):
print
"Player wins!"
else
:
print
"Player and computer tie!"
# test your code - LEAVE THESE CALLS 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
Back
|
FazBrowse Home
|
New Git URL