Assignment 1. Console Application IN PYTHON
For this assignment you are to create a class called Lab1. Inside the class you will create three methods that will modify a three-digit whole number:
sumNums - This method takes as input a three digit int and returns the sum of the three numbers. For instance, 123 would return 6 because 3 + 2 + 1 is 6
reverseNums - This method takes as input a three digit whole number and returns the number in reverse as a String. For instance, 123 would return "321"
getList - This method takes as an argument a three digit whole number and returns the numbers as an int list. Each element of the list should contain one of the numbers
In the main method prompt for a three digit number to be input.
Note:
You can use a for loop to iterate through a list as follows:
myList = [2,4,6,8,10] for nums in myList: print(nums)
Your output should be the sum of the digits, the whole number in reverse order as a String, and the int as a list.
Your output should resemble the following:
Things to Review
Things that will cost you points
Have a look at the below code. I have put comments wherever required for better understanding.
class Lab1:
# create the constructor
def __init__(self,number):
self.number = number
# create sum function
def sumNums(self):
# take variable to store sum
sum = 0
copy = self.number
# loop while numer is greater than 0
while (copy>0):
# get the last digit
rem = copy%10
# divide number by 10
copy = copy//10
# sum the digit
sum+=rem
# return sum
return sum
def reverseNums(self):
# take variable to store reverse number
rev = ""
copy = self.number
# loop while numer is greater than 0
while (copy>0):
# get the last digit
rem = copy%10
# divide number by 10
copy = copy//10
# add it to string variable
rev+=str(rem)
# return reverse
return rev
def getList(self):
# take empty list to store digits
lst = []
copy = self.number
# loop while numer is greater than 0
while (copy>0):
# get the last digit
rem = copy%10
# divide number by 10
copy = copy//10
# add it to the list
lst.append(rem)
# return list
return lst[::-1]
# take user input
number = int(input("Enter the three digit number: "))
# create object
obj = Lab1(number)
# call each method
print(obj.sumNums())
print(obj.reverseNums())
print(obj.getList())
Happy Learning!
Get Answers For Free
Most questions answered within 1 hours.