-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.py
151 lines (104 loc) · 3.66 KB
/
Day12.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
"""
Created on Mon May 31 16:04:24 2021
@author: Tahamid
"""
# =============================================================================
# Day 12
# =============================================================================
# 114. Scopes
# local scope
def drink():
volume = 100
print(volume)
drink()
#print(volume) # not defined outside the function i.e. it's only accessible in
# the function which means it has a local scope.
# global scope
volume = 1000
def drink():
volume = 100
print(f"Volume local = {volume}")
drink()
print(f"Volume global = {volume}")
# volume = 1000 is accessible anywhere because it is global
# scopes are applicable to everything not just variables
def resources():
def water():
volume = 100
print(f"Volume local = {volume}")
#water() # not accessible because water() function is local to the resources()
# function
# 115. No block scope
# functions create scopes in python. If/while/for loops dont so no block
# scopes in python like Java or C/C++
# 116. Modify global variables in a local scope
# to modify a global variable inside a local scope we have to say it's global
enemies = 0
def increaseEnemies():
global enemies
enemies += 2 # this will not work because we have not defined enemies
# INSIDE the local scope of this function
print(f"Updated number of enemies = {enemies}")
# this is bad practice. We usually do not want to modify global vars
# a better way to do this would be
def incEnemies():
return enemies + 1
increaseEnemies()
enemies = incEnemies()
print(f"Global enemies = {enemies}")
# 117. Python constants and global scope
# constants are usually upper case
PI = 3.14159
URL = ''
TWITTERNAME = ''
# Day 12 Project: Number guessing game
import random
from os import system, name
clear = lambda : system('cls' if name == 'nt' else 'clear')
logo = '''
/ __|_ _ ___ ______ |_ _| |_ ___ | \| |_ _ _ __ | |__ ___ _ _
| (_ | || / -_|_-<_-< | | | ' \/ -_) | .` | || | ' \| '_ \/ -_) '_|
\___|\_,_\___/__/__/ |_| |_||_\___| |_|\_|\_,_|_|_|_|_.__/\___|_|
'''
def checkStatus(guess, num):
if guess > num:
print(">Too high.")
elif guess < num:
print(">Too low.")
else:
print(f"\nYou got it! The answer was {guess}.")
def setDifficulty():
difficulty = input("Choose a difficulty. Type 'easy' or 'hard': ")
if difficulty == 'easy':
attempts = LIVES[difficulty]
elif difficulty == 'hard':
attempts = LIVES[difficulty]
return attempts
def playGame():
attempts = setDifficulty()
print(f"You get {attempts} attempts.")
gameOver = False
while not gameOver:
guess = int(input("Make a guess: "))
checkStatus(guess, NUMBER)
attempts -= 1
if guess == NUMBER or attempts <= 0:
print(f"\nGame over! The number was {NUMBER}.")
gameOver = True
break
else:
print("Guess again.")
print(f"\nYou have {attempts} attempts left.")
print(logo)
print("Welcome to the Number Guessing Game!")
print("\nI'm thinking of a number between 1 and 100.")
while input("Do you want to play the game? Type 'y' or 'n': ") == 'y':
clear()
NUMBER = random.randint(1, 100)
LIVES = {'easy' : 10, 'hard': 5}
playGame()
print("\nThanks for playing~")
# =============================================================================
# End of Day 12
# =============================================================================