-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (43 loc) · 1.56 KB
/
main.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 art
import random
print(art.logo)
print("Welcome to the Number Guessing Game!")
# Global constants
easy_lives = 10
hard_lives = 5
def play_round(number, tries):
'''Play a round until all lives have run out and compare user's guess against computer's number.'''
lives = tries
while lives > 0:
guess = int(input("Take a guess: "))
if guess < number:
lives -= 1
if lives >= 1:
print("Too low. Try again.")
print(f"You have {lives} lives remaining.")
elif guess > number:
lives -= 1
if lives >= 1:
print("Too high. Try again.")
print(f"You have {lives} lives remaining.")
else:
print(f"You got it! The number I was thinking of was {number}.")
return
print("Uh-oh! You've used up all your lives.")
print(f"The number I was thinking of was {number}. You lose :(")
def game():
'''Main function that loops until user doesn't want to play anymore'''
game_over = False
while not game_over:
number = random.randint(1, 100)
print("I'm thinking of a number between 1 and 100.")
level = input("Choose a level of difficulty (type 'easy' or 'hard'): ").lower()
if level == "easy":
play_round(number, 10)
elif level == "hard":
play_round(number, 5)
another_round = input("Would you like to play another round? (Type 'y' or 'n'): ")
if another_round == "n":
game_over = True
if __name__ == "__main__":
game()