0

I am trying to get the sum of the output_values, and it is giving me a traceback error

output_values = []
while True:
    user_input = input('Please type in your test grades, and follow your input by typing in "done"')
    if user_input == 'done':
        print('All done, here is your average')
        break
    ### here you will insert in the traceback error to prevent your code from crashing due to invalid input
    try:
        val_input = float(user_input)
        if val_input:
            output_values.append(user_input)
    except ValueError:
        print('You have typed in an invalid input, please type in your grade as a numerical digit')
        continue
print(output_values)
summation = sum(output_values)
1
  • Which line give you error? Commented Jul 18, 2018 at 2:22

1 Answer 1

1

You converted user_input from str to float and assign the returning value to val_input, and yet when you append to output_values you give it user_input instead of val_input, effectively discarding your conversion.

    val_input = float(user_input)
    if val_input:
        output_values.append(user_input)

Simply fix it with:

    val_input = float(user_input)
    if val_input:
        output_values.append(val_input)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.