Jump to content

Input/Output bug

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
Hay Guys!

When I execute this code, I get an error.

print("Type integer!");


total = 0;

count = 0;


while True:

    line = input("integer: ");

    if line:

        try:

            number = int(line);

            except ValueError as err:

                print(err);

                continue;

            total += number;

            count += 1;

            else:

                break;

            if count:

                print("count=", count, "total =", total, "mean=", count/total);

                


            

            


Lost!

#2
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
python needs good indentation to determine statement block boundaries. "if"/"else" and "try"/"except" must have the same indentation. Also it is not safe to use "input" to read data. It's beter to use "raw_input".

print("Type integer!")


total = 0

count = 0


while True:

    line = raw_input("integer: ")

    if line:

        try:

            number = int(line)

        except:

            print("Bad integer")

            continue

    else:

        break

    total += number

    count += 1

    print("count=", count, "total=", total, "mean=", total/count)
You also had a bug in the computation of "mean". You wrote "count/total".

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
The interactive shell tells you what line the error is on. "An error" is not helpful.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.