How to change the code below to allow user to enter in the input instead of have
ID: 3862521 • Letter: H
Question
How to change the code below to allow user to enter in the input instead of haveing it hard code and how to change the code to print out the output to 1 decimal place???
class Rectangle:
"""
Rectangle class
"""
def __init__(self, width=1, height=2):
"""
Constructor
"""
# Initializing private variables
self.__width = width;
self.__height = height;
def getWidth(self):
"""
Function that returns width of a rectangle
"""
return self.__width;
def getHeight(self):
"""
Function that returns height of a rectangle
"""
return self.__height;
def getArea(self):
"""
Function that returns area of a rectangle
"""
return self.__width * self.__height;
def getPerimeter(self):
"""
Function that returns perimeter of a rectangle
"""
return 2 * (self.__width + self.__height);
def main():
"""
Main function
"""
# Creating object
rect = Rectangle(4, 40);
# Printing data
print(" A " + str(rect.getWidth()) + " x " + str(rect.getHeight()) + " rectangle has an area of " + str(rect.getArea()) + " and a perimeter of " + str(rect.getPerimeter()) + ". ");
# Creating object
rect1 = Rectangle(3.5, 35.7);
# Printing data
print(" A " + str(rect1.getWidth()) + " x " + str(rect1.getHeight()) + " rectangle has an area of " + str(rect1.getArea()) + " and a perimeter of " + str(rect1.getPerimeter()) + ". ");
# Calling main function
main();
Explanation / Answer
def areaOfRectangle(width, height):
area = float(width) * float(height)
return area
# Asking for user input
width = input("Enter width of Rectangle: ")
height = input("Enter height of Rectangle: ")
# Printing data
print(" rectangle has an area of %.f " + str(areaOfRectangle(width,height)))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.