Question: We re going to define a few useful methods for you. Read their implementation and make sure they make sense. Save & Run Original -

Were going to define a few useful methods for you. Read their implementation and make sure they make sense.
Save & Run
Original -1 of 1
1
import json
2
3
# Repeatedly asks the user for a number between low & high (inclusive)
4
def getNumberBetween(prompt, low, high):
5
userinp = input(prompt) # ask the first time
6
7
while True:
8
try:
9
n = int(userinp) # try casting to an integer
10
if n < low:
11
errmessage = 'Must be at least {}'.format(low)
12
elif n > high:
13
errmessage = 'Must be at most {}'.format(high)
14
else:
15
return n
16
except ValueError: # The user didn't enter a number
17
errmessage = f'{userinp} is not a number.'
18
19
# If we haven't gotten a number yet, add the error message
20
# and ask again
21
userinp = input(f'{errmessage}
{prompt}')
22
23
# Spins the wheel of fortune wheel to give a random prize
24
# Examples:
25
# { "type": "cash", "text": "$950", "value": 950, "prize": "A trip to Ann Arbor!" },
26
# { "type": "bankrupt", "text": "Bankrupt", "prize": false },
27
# { "type": "loseturn", "text": "Lose a turn", "prize": false }
28
def spinWheel():
29
with open("wheel.json", 'r') as f:
30
wheel = json.loads(f.read())
31
return random.choice(wheel)
32
33
# Returns a category & phrase (as a tuple) to guess
34
# Example:
35
# ("Artist & Song", "Whitney Houston's I Will Always Love You")
36
def getRandomCategoryAndPhrase():
37
with open("phrases.json", 'r') as f:
38
phrases = json.loads(f.read())
39
40
category = random.choice(list(phrases.keys()))
41
phrase = random.choice(phrases[category])
42
return (category, phrase.upper())
43
44
# Given a phrase and a list of guessed letters, returns an obscured version
45
# Example:
46
# guessed: ['L','B','E','R','N','P','K','X','Z']
47
# phrase: "GLACIER NATIONAL PARK"
48
# returns>"_L___ER N____N_L P_RK"
49
def obscurePhrase(phrase, guessed):
50
rv =''
51
for s in phrase:
52
if (s in LETTERS) and (s not in guessed):
53
rv = rv+'_'
54
else:
55
rv = rv+s
56
return rv
57
58
# Returns a string representing the current state of the game
59
def showBoard(category, obscuredPhrase, guessed):
60
return """
61
Category: {}
62
Phrase: {}
63
Guessed: {}""".format(category, obscuredPhrase, ','.join(sorted(guessed)))
64

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!