In: Computer Science
PYTHON Program
Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values.
The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does.
This program reads in the length and width of a rectangle. It then prints the area and perimeter of the rectangle.
Enter length: 10.0
Enter width: 8.0
Length 10.0 width 8.0 area 80.0 perimeter 36.0
Create two pieces of data (other than what we have above) that would serve as input to the program; then write down what the output would be. We show a sample first. (We are ignoring here the first two lines of text that would always be displayed.)
Sample Input 0: Length 20 width 5
Expected Output for the above data
Length 20.0 width 5.0 area 100.0 perimeter 50.0
Create your own data now.
Input 1: length width Expected Output for the above data
Input 2: length width Expected Output for the above data
Before proceeding, get OK from your instructor or the tutor.
Using the Input-Processing-Output approach, write down the pseudo code for the program.
Before proceeding, get OK from your instructor or the tutor.
Convert the pseudo code to Python code. Use ideas from below.
Pseudo code
Input an integer into x
Input a number with a decimal point into x Set x to a + b
Set x to a * b
Print a, b, c
Print weight and mass annotated
Print a message
Python code
x = int(input(‘Enter a number: ‘))
x = float(input(‘Enter a number: ‘))
x = a + b
x = a * b
print(a, b, c)
print(‘weight is’, weight, ‘mass is’, mass) print(‘whatever message
you want’)
Test and debug the program. For testing, use the data you created in Step 1.
Show your source program and demonstrate it to your instructor. Get his/her signature below.
Pseudo code
Input an integer into length
Input an integer into width
set area to length * width
set perimeter to 2*(length+width)
print length width area perimeter annotated
PYTHON CODE:
# getting input length from the user
length=float(input("Enter length: "))
# getting input width from the user
width=float(input("Enter width: "))
# calculating the area of the rectangle
area= length * width
# calculating the perimeter of the rectangle
perimeter=2 * (length+ width)
# printing length, width, area, perimeter
print("Length %.1f width %.1f area %.1f perimeter %.1f"
%(length,width,area,perimeter))
SCREENSHOT FOR CODING:
SCREENSHOT FOR OUTPUT: