Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What does it do. Given the Python code below, answer the questions about it. val

ID: 3835409 • Letter: W

Question

What does it do. Given the Python code below, answer the questions about it. value = 10 class pong (): value = 15 def _ _init_ _ (self, value): value = value def bounce (self, height): self, value = height def how high (self): print ("Height =" +str (self value)+"ft") class ping (): value = 5 def _ _ init_ _ (self, value): self. value = value def bounce (self, height): ping. Value += height) def collide (self, other): other. value += self. value self. value -= value def how high (self) print ("Height =" +str (self. value)+"ft") ball1 = ping (20) ball2 = pong (30) for i in [20, 10]: ball1. bounce (i) ball 2. bounce (i) ball1. collide (ball 2) ball1 .how high() ball2. how high () (a) Explain the scope of every variable named value in the code above. (b) Make a chart to keep track of these variables. Show how the values change after each call to bounce () or collide (). (c) What is printed out by this script?

Explanation / Answer

a.) The scope of value=10 is a Global variable and it is created when the program execution starts and remains in memory till the program terminates.

value=15 is a Class Variable and it is created and can be accessed in the whole class and referenced directly from the class or from any instance in the class.

value=5 is also a Class Variable and it is referenced within the class Ping

self.value is an Instance Variable that is declaring a variable with self.(variable name) inside a function inside a class such that all class functions can access it.

value is an Instance Variable that is declaring a variable inside a function inside a class such that only that function can access it (its in that functions scope).

b.) For Ball b1 of ping : self.value=20 and Ball b2 of pong : self.value=30

1. For i=20 : Ball 1: self.value=20 and static value=5, Ball2: self.value=15 and static value=15 after ball1.bounce(): static value=25, after ball2.bounce(): self.value=20, after ball1.collide(ball2) : self.value for ball2=20+20 and self.value for ball1=20-10

2. For i=10 : Ball 1: self.value=10 and static value=25, Ball2: self.value=40 and static value=15 after ball1.bounce(): static value=35, after ball2.bounce(): self.value=10, after ball1.collide(ball2) : self.value for ball2=10+10 and self.value for ball1=10-10

c.) The above script prints the output as
Height=10ft
   Height=40ft
   Height=0ft
   Height=20ft

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote