I am starting today with a new attitude, like I stated yesterday. And it kind of feels different and exciting.
I am
excited to face the challenges ahead. Hopefully it lasts (my excitement not the
challenges to be precise). But is there a way to rid ourselves of challenges??
I do not know. What I know is that it gets easier to solve them. Maybe.
Hopefully.
Without
further ado, I wish Microsoft word gives word/sentence suggestions by the way
(totally unrelated to today’s agenda).
Today,
continuing day 12 of the prestigious 100 days of python course on Udemy, I
learned how to modify global variables in a local scope (i.e., a function) by
calling it in the function (like so: global variable). It is advised to avoid
modifying global scope because it makes your code prone to errors :(
If
you need to modify a global scope variable with a function, use the return
statement instead. Like so:
Code
from class:
enemies = 1
def increase_enemies(enemy):
print(f"enemies inside
function: {enemy}")
return
enemy + 1
enemies = increase_enemies(enemies)
print(f"enemies outside function: {enemies}")
global
constants should be upper case. It serves as a reminder to not modify it along
the line since it is different from the normal naming convection.
My
Number Guessing Game Code:
import random
import art
print(art.logo)
print("Welcome to the number guessing game!")
print("I am thinking of a number between 1 and 100")
attempts = 0
number = random.randint(1, 100)
difficulty = input("Choose a difficulty. Type 'easy' or 'hard': ").lower()
def game():
global attempts
print(f"You have {attempts} attempts remaining to guess the number.")
for attempt in range(attempts):
guess = int(input("Make a guess: "))
if guess == number:
print(f"You got it! The answer was {number}")
break
else:
attempts -= 1
if attempts != 0:
if guess < number:
print("Too low.")
print("Guess again")
else:
print("Too high.")
print("Guess again")
print(f"You have {attempts} attempts remaining to guess the number.")
else:
print(f"You've run out of guesses. You lose")
print(f"Pssst, the correct answer is {number}")
if difficulty != "easy":
attempts = 5
else:
attempts = 10game()
Let’s
not be too hard on ourselves. As long as we keep trying, we are succeeding.
I
have actually improved these past few days. Let’s not be too lenient either. We
can do this!! Definitely :)
Comments
Post a Comment