33,551 views
39 votes
39 votes
Overview

In this program, you will use incremental development to manipulate a list. Objectives Be able to:
Get a list of numbers Display the individual numbers of a list
Find the average and maximum of the numbers in a list Perform calculations on a number in a list Sort a list Description
Prompt the user for how many weights should be added to a list and get the weights (in pounds) from the user, one at a time.
Display the weights back to the user, along with the average and maximum weight.
Next, ask the user for a list location and convert the weight at that location to kilogra ms.
Next, display the sorted list. And finally, display the list of weights again, along with what the weights would be on Mars. One run of the full program is as follows:
Enter the number of weights: 4
Enter weight 1: 236.0
Enter weight 2: 89.5
Enter weight 3: 176.0
Enter weight 4: 166.3
Weights: [236.0, 89.5, 176.0, 166.3]
Average weight: 166.95
Max weight: 236.00
Enter a list location (1 - 4): 3
Weight in pounds: 176.00
Weight in kilograms: 80.00
Sorted list: [89.5, 166.3, 176.0, 236.0]
Weight on Earth: [89.5, 166.3, 176.0, 236.0]
Weight on Mars: [33.9, 62.9, 66.6, 89.3]
Think about how you would do this before you continue reading.
Come up with a very rough draft of how you would create this program.
Then see if it follows the same logic presented here.
If you do not know where to start, read the first step below and then try to construct the rest of the program on your own. Commonly, the hardest part of writing a program is knowing where to start. Try to begin without using the guide below. If you need more information on how to convert pounds to kilograms or how to convert the weight on earth to the weight on Mars, look at steps 4 and 6 below.
(1) Prompt the user for the number of weights and then prompt the user to enter the numbers, each corresponding to a person's weight in pounds. Store all weights in a list. Output the list. Ex: Enter the number of weights: 4 Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 176.0 Enter weight 4: 166.3 Weights: [236.0, 89.5, 176.0, 166.3]
(2) Output the average of the list's elements with two digits after the decimal point. Hint: Use a conversion specifier to output with a certain number of digits after the decimal point.
(3) Output the max list element with two digits after the decimal point. Ex: Enter the number of weights:
4 Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 176.0 Enter weight 4: 166.3 Weights: [236.0, 89.5, 176.0, 166.3] Average weight: 166.95 Max weight: 236.00
(4) Prompt the user for a number between 1 and the number of weights in the list. Output the weight at the user specified location and the corresponding value in kilograms. 1 kilogram is equal to 2.2 pounds. Ex: Enter a list location (1 - 4): 3 Weight in pounds: 176.00 Weight in kilograms: 80.00
(5) Sort the list's elements from least heavy to heaviest weight. Ex: Sorted list: [89.5, 166.3, 176.0, 236.0]
(6) Create another list for the weights on Mars. To compute weight on Mars, take the weight on Earth and divide by Earth's gravitational force, which is 9.81. Then multiply by the Mars gravitational force, which is 3.711. Use the built in Python function round() to round the Mars weights to 1 decimal point. Print out each set of weights as follows: Ex: If the sorted list of weights is [89.5, 166.3, 176.0, 236.0] Weight on Earth: [89.5, 166.3, 176.0, 236.0] Weight on Mars: [33.9, 62.9, 66.6, 89.3]
(7) Congratulate yourself on a job well done!

User Maviles
by
3.1k points

1 Answer

14 votes
14 votes

Answer:

The program in Python is as follows:

num_weight = int(input("Weights: "))

Weights = []

totalweight = 0

for i in range(num_weight):

w = float(input("Weight "+str(i+1)+": "))

Weights.append(w)

totalweight+=w

print("Weights: ",Weights)

print('Average Weight: %.2f' % (totalweight/num_weight))

print('Maximum Weight: %.2f' % max(Weights))

num = int(input("Enter a number between 1 and "+str(num_weight)+": "))

print('Weight in kg: %.2f' % Weights[i-1])

print('Weight in lb: %.2f' % (Weights[i-1]/2.205))

Weights.sort()

print("Sorted weights: ",Weights)

weight_on_mars = []

for i in range(num_weight):

weight_on_mars.append(round((Weights[i]/9.81 * 3.711),1))

print("Weights on mars: ",weight_on_mars)

print("Congratulations")

Step-by-step explanation:

This gets the number of weights

num_weight = int(input("Weights: "))

This initializes the weights

Weights = []

This initializes the total weight to 0

totalweight = 0

The iterates through the number of weights

for i in range(num_weight):

This gets each weight

w = float(input("Weight "+str(i+1)+": "))

This appends the weight to the list

Weights.append(w)

This calculates the total weights

totalweight+=w

This prints all weights

print("Weights: ",Weights)

Calculate and print average weights to 2 decimal places

print('Average Weight: %.2f' % (totalweight/num_weight))

Calculate and print maximum weights to 2 decimal places

print('Maximum Weight: %.2f' % max(Weights))

Prompt the user for input between 1 and the number of weights

num = int(input("Enter a number between 1 and "+str(num_weight)+": "))

Print the weight at that location in kg and lb

print('Weight in kg: %.2f' % Weights[i-1])

print('Weight in lb: %.2f' % (Weights[i-1]/2.205))

Sort weights and print the sorted weights

Weights.sort()

print("Sorted weights: ",Weights)

Create a new list for weights on mars

weight_on_mars = []

The following populates the list for weights on mars

for i in range(num_weight):

weight_on_mars.append(round((Weights[i]/9.81 * 3.711),1))

Print the populated list

print("Weights on mars: ",weight_on_mars)

print("Congratulations")

User Shahil Mohammed
by
2.9k points