-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay12.py
More file actions
135 lines (98 loc) · 3.17 KB
/
Day12.py
File metadata and controls
135 lines (98 loc) · 3.17 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Namespaces : Local Vs Global Scopes
# enimies = 1
# def increase_enimies():
# enimies = 2
# print(f"eenimies inside function: {enimies}")
# increase_enimies()
# print(f"eenimies outside function: {enimies}")
# Local Scope
# def drink_potion():
# potion_length = 2
# print(potion_length)
# drink_potion()
# Global Scope
# potion_length = 1
# player_health = 10
# def drink_potion():
# potion_length = 2
# print(potion_length)
# print(player_health)
# drink_potion()
# player_health = 10
# def game():
# def drink_potion():
# potion_length = 2
# print(player_health)
# drink_potion()
# game()
# print(player_health)
# There is no Block Scope
# game_level = 3
# enimies = ["Skeleton", "Zombie", "Alien"]
# if game_level < 5:
# new_enemy = enimies[0]
# print(new_enemy)
# Modifying Global Scope
# enimies = 1
# def increase_enimies():
# global enimies
# enimies += 1
# print(f"enimies inside function: {enimies}")
# increase_enimies()
# print(f"enimies inside function: {enimies}")
# enimies = 1
# def increase_enimies():
# print(f"enimies inside function: {enimies}")
# return enimies + 1
# print(increase_enimies())
# print(f"enimies inside function: {enimies}")
# Number Guessing Game
print("""
/ _ \_ _ ___ ___ ___ /__ \ |__ ___ /\ \ \_ _ _ __ ___ | |__ ___ _ __
/ /_\/ | | |/ _ \/ __/ __| / /\/ '_ \ / _ \ / \/ / | | | '_ ` _ \| '_ \ / _ \ '__|
/ /_\\| |_| | __/\__ \__ \ / / | | | | __/ / /\ /| |_| | | | | | | |_) | __/ |
\____/ \__,_|\___||___/___/ \/ |_| |_|\___| \_\ \/ \__,_|_| |_| |_|_.__/ \___|_|
""")
print("Welcome! to Number Guesing Game")
from random import randint
EASY_LEVEL_TURNS = 10
HARD_LEVEL_TURNS = 5
#Function to check user's guess against actual answer.
def check_answer(guess, answer, turns):
"""checks answer against guess. Returns the number of turns remaining."""
if guess > answer:
print("Too high.")
return turns - 1
elif guess < answer:
print("Too low.")
return turns - 1
else:
print(f"You got it! The answer was {answer}.")
#Make function to set difficulty.
def set_difficulty():
level = input("Choose a difficulty. Type 'easy' or 'hard': ")
if level == "easy":
return EASY_LEVEL_TURNS
else:
return HARD_LEVEL_TURNS
def game():
#Choosing a random number between 1 and 100.
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
answer = randint(1, 100)
print(f"Pssst, the correct answer is {answer}")
turns = set_difficulty()
#Repeat the guessing functionality if they get it wrong.
guess = 0
while guess != answer:
print(f"You have {turns} attempts remaining to guess the number.")
#Let the user guess a number.
guess = int(input("Make a guess: "))
#Track the number of turns and reduce by 1 if they get it wrong.
turns = check_answer(guess, answer, turns)
if turns == 0:
print("You've run out of guesses, you lose.")
return
elif guess != answer:
print("Guess again.")
game()