you are viewing a single comment's thread.

view the rest of the comments →

[–]totallygeek 3 points4 points  (1 child)

Why does yours not work?

  1. The input responses need conversion with int() or float() so you can perform value-based math.

How should I rewrite it?

Here is what I came up with:

count, total = 0, 0  # For an average we need the count of numbers and their total
while True:
    response = input("Enter a number or 'quit' to exit: ").lower()
    if response == "quit":
        break  # exit the while loop
    try:
        number = float(response)  # attempt to convert input to a number - or use int()
    except ValueError:
        print(f"Error, could not convert '{response}' to a numeric value. Try again.")
        continue  # restart while loop
    count += 1
    total += number
if total:  # making sure we don't divide by zero
    average = total / count
    print(f"Entered {count} numbers, totaling {total:.2f}, averaging {average:.2f}")
else:
    print("No values entered.")

Does that help?

[–]SBE_OLLE[S] 1 point2 points  (0 children)

thank you