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...
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...
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.
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...
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 Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once...
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once The program should output the average, largest, and smallest of 5 numbers. You must implement the methods listed below in your program. static float getAverage(int[] data) {...} static int getLargest(int[] data) {...} static int getSmallest(int[] data) {...}
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the...
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and more...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT