-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom Generator.py
More file actions
47 lines (38 loc) · 1.5 KB
/
Copy pathRandom Generator.py
File metadata and controls
47 lines (38 loc) · 1.5 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
import random
def play_round(round_number, max_attempts=7):
upper = 100 + (round_number - 1) * 50 # range becomes harder each round: 1..100, 1..150, 1..200, ...
secret = random.randint(1, upper)
attempts = 0
print(f"\nRound {round_number}: Guess the number between 1 and {upper}. You have {max_attempts} attempts.")
while attempts < max_attempts:
s = input(f"Attempt {attempts + 1}/{max_attempts} - Enter your guess: ")
try:
guess = int(s)
except ValueError:
print("Invalid input: please enter an integer. This does NOT count as an attempt.")
continue
if guess < 1 or guess > upper:
print(f"Out of range: enter a number between 1 and {upper}. This does NOT count as an attempt.")
continue
attempts += 1
if guess == secret:
print(f"Correct! You guessed the number in {attempts} attempt{'s' if attempts != 1 else ''}.")
return True
elif guess < secret:
print("Too low.")
else:
print("Too high.")
print(f"Game Over. The correct number was {secret}.")
return False
def main():
round_number = 1
while True:
play_round(round_number)
again = input("Play again? (y/n): ").strip().lower()
if again and again[0] == 'y':
round_number += 1
continue
print("Thanks for playing.")
break
if __name__ == "__main__":
main()