user1810908
user1810908

Reputation: 11

Python 3: End of File error with no message

I'm having some trouble figure out how to create an EOFError without printing something after it.

This is the section of the program I'm having trouble with:

def main():
  try:

    k = float(input("Number? "))

    newton(k)
    print("The approximate square root of", k,"is:",newton(k))
    print("The error is:",(newton(k))-(math.sqrt(k)))

  except EOFError:

    print("End of File")

I'm trying to make this so that it doesn't print anything after the user presses Ctrl+D. The program should be killed right after Ctrl+D.

I've trying doing print("") but that creates an extra space.

Thanks in advance

Upvotes: 1

Views: 1314

Answers (1)

Calvin Cheng
Calvin Cheng

Reputation: 36506

def main():
    try:

        k = float(input("Number? "))

        newton(k)
        print("The approximate square root of", k,"is:",newton(k))
        print("The error is:",(newton(k))-(math.sqrt(k)))

    except EOFError:

        pass

As a separate note, I noticed that you are using 2 spaces in your code indentation. It's a good practice to use 4 spaces instead.

Upvotes: 2

Related Questions