Skip to content

Instantly share code, notes, and snippets.

@Pinacolada64
Created November 7, 2022 22:19
Show Gist options
  • Save Pinacolada64/e1d95c3231bbd20e82f48190280641e5 to your computer and use it in GitHub Desktop.
Save Pinacolada64/e1d95c3231bbd20e82f48190280641e5 to your computer and use it in GitHub Desktop.
try/catch, random.choice
import random
def ask_name():
is_ok = False
while is_ok is False:
my_name = input("What is your name? ")
if my_name.lower() == "michelle":
print("You answered correctly!")
return my_name.title()
else:
print("Think of the person sitting to your left.")
class Character:
def __init__(self, name):
self.name = name
# just instantiating the object, fill in stuff later
self.armor = None
print(f">>> Character '{name}' instantiated.")
class Ally:
def __init__(self,name):
self.name = name
class Dict:
def __init__(self):
self.dictionary = []
# def add_fish_bird:
# ask_name()
fred = Character(name=ask_name())
fred.armor = random.choice(["brigandine", "leather", "plate mail", "wooden"])
fred.shoe_size = 10
print(f'{fred.shoe_size=} got instantiated even though not in __init__?')
print(f'{fred.name} has {fred.armor} armor.')
try:
print(f"{fred.unknown} property is not declared.")
except AttributeError:
print(f"Tried using 'fred.unknown' property but it's not declared.")
@Pinacolada64
Copy link
Author

output:

What is your name? michelle
You answered correctly!
>>> Character 'Michelle' instantiated.
fred.shoe_size=10 got instantiated even though not in __init__?
Michelle has leather armor.
Tried using 'fred.unknown' property but it's not declared.

Process finished with exit code 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment