In: Computer Science
(1) Prompt the user to enter four numbers, each corresponding to
a person's weight in pounds. Store all weights in a list. Output
the list. (2 pts)
Ex:
Enter weight 1: 236 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. (1 pt)
(3) Output the max list element. (1 pt)
Ex:
Enter weight 1: 236 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.0
(4) Prompt the user for a number between 1 and 4. Output the
weight at the user specified location and the corresponding value
in kilograms. 1 kilogram is equal to 2.2 pounds. (3 pts)
Ex:
Enter a list index (1 - 4): 3 Weight in pounds: 176.0 Weight in kilograms: 80.0
(5) Sort the list's elements from least heavy to heaviest
weight. (2 pts)
Ex:
Sorted list: [89.5, 166.3, 176.0, 236.0]
This is what I have so far:
# FIXME (1): Prompt for four weights. Add all weights to a list.
Output list.
weights = []
for i in range(4):
w = float(input("Enter weight "+str(i+1)+":"))
weights.append(w)
print("\nWeights:",weights)
# FIXME (2): Output average of weights.
avg = sum(weights) / float(len(weights))
print("Average weight:",avg)
# FIXME (3): Output max weight from list.
print("Max weight:",max(weights))
# FIXME (4): Prompt the user for a list index and output that
weight in pounds and kilograms.
index = int(input("\nEnter a list index location (1 - 4):
\n"))
print("Weight in pounds:",weights[index-1])
print("Weight in Kilograms:",weights[index-1]/2.2)
# FIXME (5): Sort the list and output it.
weights.sort()
print("\nSorted list:",weights)
Your output starts with
Enter weight 1:
Weights: [236.0]
Enter weight 2:
Weights: [236.0, 89.5].
Enter weight 3:
Weights: [236.0, 89.5, 176.0]
Enter weigt 4:
Weights: [236.0, 89.5, 176.0, 166.3]
Expected output starts with
Enter weight 1:
Enter weight 2:
Enter weight 3:
Enter weight 4:
Weights: [236.0, 89.5, 176.0, 166.3]
How do I fix this!?
It seems to be an indentation problem in the code, due to which print statement is executing after each time you ask for a new weight.. You should print all the weights once you have got all the inputs.. The correct code will be below:
# FIXME (1): Prompt for four weights. Add all weights to a list. Output list. weights = [] for i in range(4): w = float(input("Enter weight "+str(i+1)+": ")) weights.append(w) print("\nWeights:",weights) # FIXME (2): Output average of weights. avg = sum(weights) / len(weights) print("Average weight:",avg) # FIXME (3): Output max weight from list. print("Max weight:", max(weights)) # FIXME (4): Prompt the user for a list index and output that weight in pounds and kilograms. index = int(input("\nEnter a list index location (1 - 4): \n")) print("Weight in pounds:",weights[index-1]) print("Weight in Kilograms:",weights[index-1]/2.2) # FIXME (5): Sort the list and output it. weights.sort() print("\nSorted list:",weights)
************************************************** Please refer to screenshot above for the indentation.. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.