0

I was wondering why this version of a code doesn't work

total = 0
count = 0

while True:
    inp1 = input("What is your number?\n")
    if inp1 == 'done' or 'Done':
        break
    try:
        number = float(inp1)
    except:
        print("Invalid number")
        continue
    total = total + number
    count = count + 1
print(total, count, total/count)

but when I remove the or "Done" it works?

total = 0
count = 0

while True:
    inp1 = input("What is your number?\n")
    if inp1 == 'done':
        break
    try:
        number = float(inp1)
    except:
        print("Invalid number")
        continue
    total = total + number
    count = count + 1
print(total, count, total/count)

For the first code, the error "ZeroDivisionError: division by zero" appears. But why does adding an extra or cause my loop to be circumvented?

mkrieger1
  • 14,486
  • 4
  • 43
  • 54

0 Answers0