-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
92 lines (67 loc) · 2.12 KB
/
functions.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
# This file contains many functions to be used in the main file
import random # Allows randomizing things such as lists and strings
# List of valid choices when the player is asked for their choice
possibleChoices = (
"r", # Rock
"p", # Paper
"s", # Scissors
)
# The player's and the computer's starting scores
playerScore = 0
computerScore = 0
def addPointPlayer(): # Adds a point for the player
global playerScore
playerScore = playerScore + 1
def addPointComputer(): # Adds a point for the computer
global computerScore
computerScore = computerScore + 1
def scores(): # Retreives the scores and prints them
print("Player's score: " + str(playerScore))
print("Computer's score: " + str(computerScore))
def playerLoss(): # Response if the player loses
addPointComputer()
print("You lost! One point to the computer!")
scores()
def playerWin(): # Response if the player wins
addPointPlayer()
print("You won! One point to you!")
scores()
def tied(): # Response if the player and the computer ties
print("Tied! No points awarded.")
def playerChoice(): # Asks the player for their choice
global playerAction
playerAction = input(
"""
Rock, paper, or scissors?
Acceptable responses are...
"r": Chooses rock.
"p": Chooses paper.
"s": Chooses scissors.
"q": Quits the game.
"""
)
def computerChoice(): # Gives the computer a random choice from the possibleChoices list
global computerAction
computerAction = random.choice(possibleChoices)
computerAction = computerAction
def playRps(): # Compares the choices and determines who won the game
game = "".join([playerAction, computerAction])
outcomes = {
"rr": tied,
"pp": tied,
"ss": tied,
"rp": playerLoss,
"ps": playerLoss,
"sr": playerLoss,
"rs": playerWin,
"pr": playerWin,
"sp": playerWin,
}
if playerAction == "q":
return True
else:
action = outcomes.get(game)
if action:
action()
else:
print("Invalid input!")