-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanceguessing.py
More file actions
62 lines (51 loc) · 1.92 KB
/
Copy pathadvanceguessing.py
File metadata and controls
62 lines (51 loc) · 1.92 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
import random
import time
def play_game():
lowest_no = 1
highest_no = 100
max_attempts = 10
score = 100
answer = random.randint(lowest_no, highest_no)
guesses = 0
start_time = time.time()
print("Number guessing game")
print(f"Select a number between {lowest_no} and {highest_no}")
print(f"You have {max_attempts} attempts!")
while guesses < max_attempts:
guess = input("Enter your guess: ")
if guess.strip() == "":
print("You entered nothing!😪")
continue
if guess.isdigit():
guess = int(guess)
guesses += 1
if guess < lowest_no or guess > highest_no:
print("That number is out of range🤨")
print(f"Please select a number between {lowest_no} and {highest_no}")
elif guess < answer:
print("Too low! Try again")
elif guess > answer:
print("Too high! Try again")
else:
end_time = time.time()
print(f"CORRECT! The answer was {answer}")
print(f"Number of guesses: {guesses}")
print(f"You took {end_time - start_time:.2f} seconds!")
score = max(100 - guesses * 10, 0)
print(f"Your score: {score}")
break
if guesses == 5: # Give a hint after 5 guesses
if answer % 2 == 0:
print("Hint: The number is even.")
else:
print("Hint: The number is odd.")
else:
print("Invalid guess!😪")
print(f"Please select a number between {lowest_no} and {highest_no}")
if guesses >= max_attempts:
print(f"Game Over! The correct answer was {answer}")
while True:
play_game()
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != 'yes':
break