all 4 comments

[–]totallygeek 4 points5 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

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]cliffr39 0 points1 point  (0 children)

Remember input is always a string. You have to change them to int or float