Question

In: Computer Science

(1) Prompt the user to enter four numbers, each corresponding to a person's weight in pounds....

(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!?

Solutions

Expert Solution

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.


Related Solutions

Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in...
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in an array of doubles. Output the array's numbers on one line, each number followed by one space. (2 pts) Ex: Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 142.0 Enter weight 4: 166.3 Enter weight 5: 93.0 You entered: 236.0 89.5 142.0 166.3 93.0 (2) Also output the total weight, by summing the array's elements. (1 pt) (3) Also output the...
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Language C++ Ask the user to enter their weight (in pounds) and their height in inches....
Language C++ Ask the user to enter their weight (in pounds) and their height in inches. Calculate their Body Mass Index (BMI) and tell them whether they are underweight, overweight, or at optimal weight. BMI formula: weight * 703 / (height * height) Optimal weight is a BMI from 19 to 26. Lower is underweight, higher is overweight. Prompts: Enter your weight (in pounds): [possible user input: 144] Enter your height (in inches): [posible user input: 73] Possible Outputs: Your...
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
Prompt user to enter 2 integer numbers from console, compare and display these 2 numbers from...
Prompt user to enter 2 integer numbers from console, compare and display these 2 numbers from small to great. This is required to be done in MIPS assembly language.
JAVA You will then prompt the user to enter each grocery item and store it in...
JAVA You will then prompt the user to enter each grocery item and store it in your array. Afterward, the program will ask the user to enter which grocery item they are looking for in the list, and return a message back on whether it was found or not found. (Hint: You will need two for-loops for this program - one for storing each element into the array and one for searching back through the array.) See below for example...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT