-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
65 lines (51 loc) · 1.78 KB
/
game.py
File metadata and controls
65 lines (51 loc) · 1.78 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
"""
I’m thinking of a number between 1 and 100…
What is it?
It’s 50! But what if it were more random?
In a file called game.py, implement a program that:
Prompts the user for a level, n. If the user does not input a positive integer, the program should prompt again.
Randomly generates an integer between 1 and
, inclusive, using the random module.
Prompts the user to guess that integer. If the guess is not a positive integer, the program should prompt the user again.
If the guess is smaller than that integer, the program should output Too small! and prompt the user again.
If the guess is larger than that integer, the program should output Too large! and prompt the user again.
If the guess is the same as that integer, the program should output Just right! and exit.
"""
import random
def get_level():
while True:
try:
level = int(input("Level: "))
random_number = random.randint(1, level)
except ValueError:
pass
# print("Value is not an integer!")
else:
return level, random_number
def get_guess():
while True:
try:
guess = int(input("Guess: "))
except ValueError:
pass
# print("Value is not an integer!")
else:
return guess
def main():
level, random_number = get_level()
guess = get_guess()
while True:
# if guess > level:
# # print("Out-of-range level.")
# guess = get_guess()
if guess == random_number:
print("Just right!")
break
elif guess > random_number:
print("Too large!")
guess = get_guess()
else:
print("Too small!")
guess = get_guess()
if __name__ == "__main__":
main()