It took five days to come back to studying routine. Blackjack project was overwelming :(
I realised that studying bit by bit everyday is more important than studying a lot in one day and having five days off. I forgot small details and had to spend time to recap things.
exist within a function
only valid within the walls of this function
valid anywhere within the python file
valid in certain scope
e.g.
play_health = 10
def game():
def drink_potion():
potion_strength = 2
print(player_health)
drink_potion() #-> nested function,
drink_postion() is nested two levels deep
it only can be called inside the game() function
There is no block scope in python unlike Java, C++
variables within if, else, for, while code blocks with colon and indentation, they don't count as creating a local scope/ fence. Unless within the function, it has a global scope.
Modifying a Global Variable: Avoid!
Global constants: Variable that you will never ever change.
naming convention: all upper case.
PI = 3.14159
It was my first solo flying project, which I wrote from scrath without Angela's direction. I started writing down requirements lists and flow chart.
-My flow chart looks very tacky but I will leave it here for future reference to see how much I progressed lol
-I struggled with scope of variables when I tried to use multiple functions, so I put everything in one function. However, Angela tackled it somehow. hmmmm Mine worked fine but gotta try one more time by using multiple ifs.
- Sentences collecting
-You have # attempts remaining to guess the number.
-Choose a difficulty.
#-----------------------------<My code>----------------------------
logo = """
_ _ _ _____ _ _____
| \ | | | | / ____| (_) / ____|
| \| |_ _ _ __ ___ | |__ ___ _ __ | | __ _ _ ___ ___ ___ _ _ __ __ _ | | __ __ _ _ __ ___ ___
| . ` | | | | '_ ` _ \| '_ \ / _ \ '__| | | |_ | | | |/ _ \/ __/ __| | '_ \ / _` | | | |_ |/ _` | '_ ` _ \ / _ \
| |\ | |_| | | | | | | |_) | __/ | | |__| | |_| | __/\__ \__ \ | | | | (_| | | |__| | (_| | | | | | | __/
|_| \_|\__,_|_| |_| |_|_.__/ \___|_| \_____|\__,_|\___||___/___/_|_| |_|\__, | \_____|\__,_|_| |_| |_|\___|
__/ |
|___/
"""
#from art import logo
import random
answer = random.randint(1, 100)
EASY = 10
HARD = 5
attempt = 0
def guessing():
answer = random.randint(0, 100)
#print(f"\n pssst answer is {answer}")
print(logo)
print("I'm thinking of a number between 1 and 100.")
level = input("Choose a difficulty level 'easy' or 'hard'? ")
if level == 'easy':
attempt = EASY
else:
attempt = HARD
while attempt > 0:
guess = int(input("Make a guess: "))
if guess == answer:
break
elif guess > answer:
attempt -= 1
print( f"Too high. You have {attempt} attempts left")
elif guess < answer:
attempt -= 1
print(f"Too low. You have {attempt} attempts left")
if attempt > 0 and guess == answer:
print("\n Congrats. You guessed it! \n")
elif attempt == 0 and guess != answer:
print("\n Out of attempts. You lose. \n")
if input("Restart? y or n: ") == "y":
guessing()
guessing()
Read "🚨" part carefully.
function, return: output or exit the function, assign empty variable to be able to use it, using same variable name to contain different output in the flow of the code... omg so confusing here.
It will take some time to get use to it....
from random import randint
#generate random number
EASY_LEVEL = 10
HARD_LEVEL = 5
def check_answer(guess, answer, turns):
""" chekcs answer against guess, returns the number of turns remaining """
if guess == answer:
print(f"Congrats. Answer was {answer}")
if input("restart? y or n: ") == "y":
game()
return
elif guess > answer:
print("Too high")
return turns -1 # 🚨 before, I used += which is bound to variable
elif guess < answer:
print("Too low")
return turns -1
#set difficulty and attempts
def difficulty():
level = input("Choose a difficulty. easy or hard?: ")
# 🚨 in this stage, creating global constants
if level == "easy":
return EASY_LEVEL
""" 🚨 if I create variable'turns' using =, inside function,
it's no use as it's local, instead,
use return so I can use it whenever I call this function:
set global var and assign the function to get output."""
print("10 attempts")
else:
return HARD_LEVEL
print("5 attempts")
def game():
answer = randint(1, 100)
print("welcome")
print(answer)
#loop user guess - compare - result if they get it wrong. if get it right, finish.
turns = difficulty()
"""🚨setting local variable turns,output of difficult()
is EASY_LEVEL OR HARD_LEVEL
which contains 10, 5 each and
it's number of turns."""
guess = 0 """ 🚨 when creating while loop,
guess is defined after while,
so make an empty one first.
it will be only used once. """
while guess != answer and turns != 0:
#get user guess
print(f"you have {turns} attempts remaining to guess the number.")
guess = int(input("Guess a number btw 1 to 100: "))
#compare guess with answer #show result
#track thenumber of turns, reduce by 1 if they get it wrong
turns = check_answer(guess, answer, turns) # 🚨check_answer returns output: turns -1
if turns == 0:
print("you've run out of guess the number")
if input("restart? y or n: ") == "y":
game()
return # 🚨 to exit and end function
game()
A guessing game is a popular and engaging activity that involves players trying to identify something based on limited information or clues. This type of game can vary widely, from guessing a number or word to identifying a person or place. One popular setting for such games is online platforms, where players can compete or collaborate in virtual environments. In the context of South Korean online gaming, 메이저사이트 (major sites) are well-known for offering a variety of guessing games and other interactive content. These 메이저사이트 ensure a high-quality gaming experience with reliable software and fair play. Whether it's a simple guessing game or a more complex trivia challenge, 메이저사이트 provide a fun and competitive atmosphere for players to test their knowledge and intuition. As technology advances, these platforms continue to innovate, enhancing the guessing game experience with new features and interactive elements.
On Python Basics Day 12, the focus shifts to understanding scope, a fundamental concept in programming. Scope defines the accessibility of variables within a program. Learning about local, global, and nonlocal scopes is crucial for writing efficient and bug-free code. By grasping scope rules, programmers gain better control over their code's behavior, akin to mastering different strategies in a 카지노사이트 game.