When I print my function to print the return values, it is asking for all the inputs all over again, when it should only ask one time

Question:

When I run this program it should ask for the weight 4 times, and return the values from the other functions. But once I enter the value 4 times, it re-runs it and asks for all the values again.

def create_list():
    my_list = []
    input_number = 1
    while input_number < 5:
        num = float(input(f'Enter weight {input_number}:n'))
        my_list.append(num)
        input_number += 1
    return my_list


def average_weight(list):
    total = 0
    weight_list = list
    for i in range(0, len(list)):
        total = total + list[i]
    average = total / len(list)
    summary = f'Weights entered: {weight_list}nAverage weight: {average}'
    return summary


def pound_to_kilo(pound):
    index_location = int(input("Enter a list index location (0 - 3):n"))
    weight = 0
    if index_location == 0:
        weight = pound[0]
    elif index_location == 1:
        weight = pound[1]
    elif index_location == 2:
        weight = pound[2]
    elif index_location == 3:
        weight = pound[3]
    else:
        weight = "Invalid index range"
    pounds = weight
    kilograms = pounds / 2.2
    return f'Weight in pounds: {pounds:.2f}nWeight in kilograms: {kilograms:.2f}'


print(average_weight(create_list()))
print(f"Max weight: {max(create_list()):.2f}")
print(pound_to_kilo(create_list()))

I am not sure how to fix this.

Example output should be like this:

Enter weight 1:
>>>236.0
Enter weight 2:
>>>89.5
Enter weight 3:
>>>176.0
Enter weight 4:
>>>166.3
Weights entered: [236.0, 89.5, 176.0, 166.3]
Average weight: 166.95
Max weight: 236.00
Enter a list index location (0 - 3):
>>>2
Weight in pounds: 176.00
Weight in kilograms: 80.00
Asked By: Zk Xi

||

Answers:

Each time you call create_list it’s going to create a new list (like it says on the tin).

If you don’t want to create a new list each time you call your other functions, you want to call create_list once and then reference the resulting list in subsequent function calls.

items = create_list()
print(average_weight(items))
print(f"Max weight: {max(items):.2f}")
print(pound_to_kilo(items))
Answered By: Samwise
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.