-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasdw
96 lines (77 loc) · 2.9 KB
/
asdw
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
86
87
88
89
90
91
92
93
94
95
96
import random
def guess_the_number():
# Ask the player to choose the range of numbers to guess from
min_num = int(input("Enter the minimum number in the range: "))
max_num = int(input("Enter the maximum number in the range: "))
# Ask the player to choose the difficulty level
difficulty = int(input("Choose the difficulty level (1-3): "))
if difficulty == 1:
range_size = max_num - min_num + 1
elif difficulty == 2:
range_size = (max_num - min_num + 1) * 2
elif difficulty == 3:
range_size = (max_num - min_num + 1) * 4
else:
print("Invalid difficulty level. Using default difficulty (1).")
range_size = max_num - min_num + 1
# Generate a random number between the chosen range
secret_num = random.randint(min_num, min_num + range_size)
# Ask the player to guess the number
guess = int(input("Guess a number between %d and %d: " % (min_num, min_num + range_size)))
# Keep track of the number of guesses
num_guesses = 1
# Keep guessing until the player guesses the right number
while guess != secret_num:
if guess < secret_num:
print("Your guess is too low. Try again.")
else:
print("Your guess is too high. Try again.")
# Get the player's next guess
guess = int(input("Guess a number between %d and %d: " % (min_num, min_num + range_size)))
num_guesses += 1
# Print a success message
print(f"Congratulations! You guessed the number in {num_guesses} guesses.")
# Record the player's score if they got the lowest number of guesses
try:
with open("scores.txt", "r") as f:
scores = [tuple(line.strip().split()) for line in f]
except:
scores = []
player_name = input("Enter your name: ")
scores.append((player_name, num_guesses))
scores.sort(key=lambda x: x[1])
with open("scores.txt", "w") as f:
for name, score in scores:
f.write(f"{name} {score}\n")
# Display the leaderboard
print("\nLeaderboard:")
for i, (name, score) in enumerate(scores):
print(f"{i+1}. {name}: {score}")
# Update the player's win/loss record
try:
with open("record.txt", "r") as f:
wins, losses = map(int, f.read().split())
except:
wins, losses = 0, 0
if num_guesses == 1:
print("Wow, you got it on the first try! Impressive!")
wins += 1
else:
losses += 1
with open("record.txt", "w") as f:
f.write(f"{wins} {losses}")
# Display the player's win/loss record
print(f"\nRecord: {wins}-{losses}")
# Calculate and display the player's average number of guesses per round
total_guesses = wins + losses
if total_guesses > 0:
average_guesses = sum(score for _, score in scores) / total_guesses
print(f"Average guesses per round: {average_guesses:.2f}")
# Ask the player if they want to play again
play_again = input("\nDo you want to play again? (y/n) ")
if play_again == 'y':
guess_the_number()
else:
print("\nThank you for playing!")
# Start the game
guess_the_number()