Skip to content

Instantly share code, notes, and snippets.

@rchurch4
Last active April 16, 2023 21:48
Show Gist options
  • Save rchurch4/c93b7d09e39990cf1440a9ebb9631e38 to your computer and use it in GitHub Desktop.
Save rchurch4/c93b7d09e39990cf1440a9ebb9631e38 to your computer and use it in GitHub Desktop.
Sample program for homework 3. Understanding scope and bindings.
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