-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek06_game_pack.py
More file actions
82 lines (67 loc) · 1.99 KB
/
Copy pathweek06_game_pack.py
File metadata and controls
82 lines (67 loc) · 1.99 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Week 6: Game Pack (Functions)
# Choose a game from a menu.
import random
def ask_int(prompt, min_val, max_val):
"""Ask for an integer between min_val and max_val (inclusive)."""
while True:
text = input(prompt)
if not text.isdigit():
print("Please type digits only.")
continue
value = int(text)
if value < min_val or value > max_val:
print("Please enter a number from", min_val, "to", max_val)
continue
return value
def play_guess_number():
print()
print("=== GUESS THE NUMBER ===")
secret = random.randint(1, 10)
max_attempts = 7
attempts = 0
while attempts < max_attempts:
guess = ask_int("Guess (1-10): ", 1, 10)
attempts = attempts + 1
if guess == secret:
print("Correct! You won in", attempts, "tries.")
return
elif guess < secret:
print("Too low!")
else:
print("Too high!")
print("Game over! The number was", secret)
def play_training_drills():
print()
print("=== TRAINING DRILLS ===")
score = 0
rounds = 5
for i in range(rounds):
print("Round", i + 1, "of", rounds)
move = input("Type YES to dodge, NO to stop: ").strip().lower()
if move == "yes":
score = score + 1
print("+1 point")
elif move == "no":
print("+0 points")
else:
score = score - 1
print("Invalid move! -1 point")
print("Score:", score)
print("---")
print("Final score:", score)
def main_menu():
while True:
print()
print("PYTHON GAME PACK")
print("1) Guess the Number")
print("2) Training Drills")
print("3) Exit")
choice = ask_int("Choose 1-3: ", 1, 3)
if choice == 1:
play_guess_number()
elif choice == 2:
play_training_drills()
else:
print("Goodbye!")
break
main_menu()