Advertisement
juanmartinvk

PCC - Object-oriented Programming Practice Quiz

Dec 25th, 2019
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. # “If you have an apple and I have an apple and we exchange these apples, then
  2. # you and I will still each have one apple. But if you have an idea and I have
  3. # an idea and we exchange these ideas, then each of us will have two ideas.”
  4. # George Bernard Shaw
  5.  
  6. class Person:
  7.     apples = 0
  8.     ideas = 0
  9.  
  10. johanna = Person()
  11. johanna.apples = 1
  12. johanna.ideas = 1
  13.  
  14. martin = Person()
  15. martin.apples = 2
  16. martin.ideas = 1
  17.  
  18. def exchange_apples(you, me):
  19.   #"you" and "me" will exchange ALL our apples with one another
  20.   you_apples = you.apples
  21.   me_apples = me.apples
  22.   you.apples = me_apples
  23.   me.apples = you_apples
  24.   return you.apples, me.apples
  25.    
  26. def exchange_ideas(you, me):
  27.   #"you" and "me" will share our ideas with one another
  28.   you_ideas = you.ideas
  29.   me_ideas = me.ideas
  30.   you.ideas += me_ideas
  31.   me.ideas += you_ideas
  32.   return you.ideas, me.ideas
  33.  
  34. exchange_apples(johanna, martin)
  35. print("Johanna has {} apples and Martin has {} apples".format(johanna.apples, martin.apples))
  36. exchange_ideas(johanna, martin)
  37. print("Johanna has {} ideas and Martin has {} ideas".format(johanna.ideas, martin.ideas))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement