Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need help for python, I cant get it to calculate

I really don’t know what I am doing wrong here but whenever I run the code the output doesn’t calculate the variable and only sets it to 0.0

total = 0.0
num1 = 0.0
average = 0.0

while True:
    input_num = input("Enter a number ")

    if input_num == 'done':
        break

    try:
        num = float(input_num)
    except ValueError:
        print("Invalid Input")
        continue

        total = total + num
        num1 = num1 + 1
        average = total / num1

print("total: ", total)
print("count: ", num1)
print("average: ", average)

I got the following after running the code
[Image of code run]: (https://imgur.com/a/lEz6ibh)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your code isn’t indented properly, so the code after continue never gets executed. To get it to work, unindent the lines after continue:

total = 0.0
num1 = 0.0
average = 0.0

while True:
    input_num = input("Enter a number ")

    if input_num == 'done':
        break

    try:
        num = float(input_num)
    except ValueError:
        print("Invalid Input")
        continue

    total = total + num
    num1 = num1 + 1
    average = total / num1

print("total: ", total)
print("count: ", num1)
print("average: ", average)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading