-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguessinggame.py
52 lines (35 loc) · 1.04 KB
/
guessinggame.py
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
import random
def main():
show_header()
play_game()
def show_header():
print("------------------------------")
print(" M&M guessing game!")
print("------------------------------")
print("Guess the number of M&Ms and you get lunch on the house!")
print()
def get_guess_from_user():
guess_text = input("How many M&Ms are in the jar? ")
guess = int(guess_text)
return guess
def evaluate_guess(guess, actual_count):
if actual_count == guess:
print(f"You got a free lunch! It was {guess}.")
elif guess < actual_count:
print("Sorry, that's too LOW!")
else:
print("That's too HIGH!")
return actual_count == guess
def play_game():
mm_count = random.randint(1, 100)
attempt_limit = 5
attempts = 0
while attempts < attempt_limit:
guess = get_guess_from_user()
attempts += 1
won = evaluate_guess(guess, mm_count)
if won:
break
print(f"Bye, you're done in {attempts} attempts!")
if __name__ == "__main__":
main()