Last active
April 16, 2023 21:48
-
-
Save rchurch4/c93b7d09e39990cf1440a9ebb9631e38 to your computer and use it in GitHub Desktop.
Sample program for homework 3. Understanding scope and bindings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random as r | |
class Wand: | |
def __init__(self, length, wood, core): | |
self.length = length, | |
self.wood = wood | |
self.core = core | |
self.person = 'Nobody' | |
def set_owner(self, person): | |
self.person = person | |
def __str__(self): | |
return 'This wand is {} inches long, made of {}, with a core of {}. {} owns this wand.'\ | |
.format(self.length, self.wood, self.core, self.person) | |
woods = ['oak', 'elder', 'cherry'] | |
wands = [] | |
def main(): | |
cores = ['phoenix feather', 'dragon heartstring', 'unicorn hair'] | |
wizards = [...] | |
for wizard in wizards: | |
wand = Wand(length=r.randrange(6,13), wood=r.choice(woods), core=r.choice(cores)) | |
wand.set_owner(person=wizard) | |
wands.append(wand) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment