- Day 12 Goals
- Namespaces: Local vs Global
- Block Scope (python doesn’t use it)
- Coding Exercise: Prime number checker
- Modifying Global Variables (Hint: don’t)
- Constants and Global Scope
- Project: Number Guessing Game
Coding Exercise: Prime Numbers
print("=" * 5)
def is_prime(num):
if num == 1:
return False
if num == 2:
return True
for i in range(2,num):
if num % i == 0:
return(False)
return True
print(is_prime(73))
Project: Number Guessing Game. ASCII art courtesy of https://patorjk.com/software/taag/
main.py
import random
from art import logo
# TO DO: Generate a random number between 1 and 100
rand=random.randint(1,100)
print(rand)
# TO DO: Get the users input and compare to the number
def guess_number(random_number, no_of_attempts):
guess = int(input("Guess a number between 1 and 100: "))
if guess > random_number:
print("Too High")
no_of_attempts -= 1
return no_of_attempts
elif guess < random_number:
print("Too Low!")
no_of_attempts -= 1
return no_of_attempts
else:
print("You got it!")
no_of_attempts = -1
return no_of_attempts
# Get the users choice of difficulty
def choose_easy_or_hard():
get_level = input("Choose a difficulty. Type 'easy' or 'hard': ")
if get_level == "easy":
no_of_attempts = 10
return no_of_attempts
else:
no_of_attempts = 5
return no_of_attempts
# set a loop to play again
play_again = "y"
while play_again == "y":
# Print the logo
print(logo)
# get the difficulty and set the number of guesses
attempts = choose_easy_or_hard()
while attempts > 0:
attempts = guess_number(rand, attempts)
if attempts == 0:
print(f"You've run out of guesses. The number was {rand} you lose.")
elif attempts == -1:
print("You've guessed the number")
else:
print(f"You have {attempts} attempts remaining to guess the number.")
play_again = input("Do you want to play again? Type 'y' or 'n': ").lower()
art.py
logo = r"""
██████╗ ██╗ ██╗███████╗███████╗███████╗ ████████╗██╗ ██╗███████╗ ███╗ ██╗██╗ ██╗███╗ ███╗██████╗ ███████╗██████╗
██╔════╝ ██║ ██║██╔════╝██╔════╝██╔════╝ ╚══██╔══╝██║ ██║██╔════╝ ████╗ ██║██║ ██║████╗ ████║██╔══██╗██╔════╝██╔══██╗
██║ ███╗██║ ██║█████╗ ███████╗███████╗ ██║ ███████║█████╗ ██╔██╗ ██║██║ ██║██╔████╔██║██████╔╝█████╗ ██████╔╝
██║ ██║██║ ██║██╔══╝ ╚════██║╚════██║ ██║ ██╔══██║██╔══╝ ██║╚██╗██║██║ ██║██║╚██╔╝██║██╔══██╗██╔══╝ ██╔══██╗
╚██████╔╝╚██████╔╝███████╗███████║███████║ ██║ ██║ ██║███████╗ ██║ ╚████║╚██████╔╝██║ ╚═╝ ██║██████╔╝███████╗██║ ██║
╚═════╝ ╚═════╝ ╚══════╝╚══════╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝╚═╝ ╚═╝
"""