-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame 2: GuessTheNumber
85 lines (77 loc) · 3.12 KB
/
Game 2: GuessTheNumber
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
import simplegui, random, math
low, high, attempts = 0, 100, 0
attemptsTotal = math.ceil(math.log((high-low),2))
secretNum = random.randrange(low, high)
def init():
print("Started new game in range 0 - 100")
print("You have " + str(attemptsTotal - attempts) + " attempts left!")
print("")
def range100():
global low, high, attempts, attemptsTotal, secretNum
low, high, attempts = 0, 100, 0
attemptsTotal = math.ceil(math.log((high-low),2))
secretNum = random.randrange(low, high)
print("Started new game in range 0 - 100")
print("You have " + str(attemptsTotal - attempts) + " attempts left!")
print("")
l1.set_text("Attempts left: " + str(attemptsTotal - attempts))
def range1000():
global low, high, attempts, attemptsTotal, secretNum
low, high, attempts = 0, 1000, 0
attemptsTotal = math.ceil(math.log((high-low),2))
secretNum = random.randrange(low, high)
print("Started new game in range 0 - 1000")
print("You have " + str(attemptsTotal - attempts) + " attempts left!")
print("")
l1.set_text("Attempts left: " + str(attemptsTotal - attempts))
def get_input(guess):
global secretNum, low, high, attempts
if guess != "":
attempts = attempts + 1
print("Your attempt no " + str(attempts) + " was: " + guess)
if float(guess) < secretNum:
if attemptsTotal != attempts:
print("Go higher!")
print("You have " + str(attemptsTotal - attempts) + " attempts left!")
else:
print("Game over! You used all of your attempts!")
print("The secret number was: " + str(secretNum))
print("_________________________")
secretNum = random.randrange(low, high)
attempts = 0
print("")
iGuess.set_text("")
elif float(guess) > secretNum:
if attemptsTotal != attempts:
print("Go lower!")
print("You have " + str(attemptsTotal - attempts) + " attempts left!")
else:
print("Game over! You used all of your attempts!")
print("The secret number was: " + str(secretNum))
print("_________________________")
secretNum = random.randrange(low, high)
attempts = 0
print("")
iGuess.set_text("")
elif float(guess) == secretNum:
print("Correct! The secret number was: " + str(secretNum))
print("You made it in " + str(attempts) + " attempts!")
print("_________________________")
secretNum = random.randrange(low, high)
attempts = 0
print("")
iGuess.set_text("")
l1.set_text("Attempts left: " + str(attemptsTotal - attempts))
frame = simplegui.create_frame("Guess the number", 0, 300)
l0 = frame.add_label("Select the range to restart:")
frame.add_button("Range: 0 - 100", range100, 120)
frame.add_button("Range: 0 - 1000", range1000, 120)
frame.add_label("")
frame.add_label("")
l1 = frame.add_label("Attempts left: " + str(attemptsTotal - attempts))
frame.add_label("")
frame.add_label("")
iGuess = frame.add_input("Enter your guess:", get_input, 100)
frame.add_label("")
frame.start()
init()