Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Pyhton please! 2 9 . 2 0 LAB: Swap two numbers Complete the swap ( ) method to exchange the values of the num attribute

Pyhton please! 29.20 LAB: Swap two numbers
Complete the swap() method to exchange the values of the num attribute of two Number objects, num1 and num2.
Hint: Refer to the given Number class to see the definitions of num attribute and other instance methods available.
Ex: If num1 is 19 and num2 is 178, calling swap(num1, num2) will swap the values so that num1 becomes 178 and num2 becomes 19.
class Number:
def __init__(self, n):
self.num = n
def get_num(self):
return self.num
def set_num(self, n):
self.num = n
def swap(num1, num2):
num1.num = self.num
num2.num = num1.num
num1.num = num2.num
if __name__=="__main__":
int1= int(input())
int2= int(input())
num1= Number(int1)
num2= Number(int2)
swap(num1, num2)
print('num1=',num1.get_num(),'and num2=',num2.get_num())

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions