python Catching several errors at once

Question:

I am trying to do this Python error handling task and I did all I can but it is not printing what it is supposed to. Can anybody help me determine what I am missing? Thank you so much!

Assignment:
This exercise combines several normal error scenarios into one program. In this exercise, create a program which prompts the user for a file name. Based on user input, open the given file and read the contents into one big string. Then convert this string to an integer and divides the number 1000 with the number. Finally, print out the result from the division.

The idea here is that no matter what the user input is, the program works. If the file cannot be found, the program prints "There seems to be no file with that name.". If the conversion fails, print "The file contents are unsuitable.", in other errors, print "There was a problem with the program.", or if everything went correctly, print "The result was [result].". In any case (besides KeyboardInterruption with Ctrl+C), the program should be impossible to break with user input. If everything works as intended, it prints the following output:

>>> 

Give the file name: hahaha...NO

There seems to be no file with that name.

>>> 

Give the file name: notebook.txt

The file contents were unsuitable.

>>> 



Give the file name: number.txt

The result was 3.194888178913738

>>> 

My code:

def getfilename():
      filename = input("Give the file name: ")
      return filename

def main():
      returned=getfilename()
      
      try:
          handle = open(returned,"r")
          filetext = handle.read()
          result=int(1000/filetext)
      except IOError:
          print ("There seems to be no file with that name.")
      except (TypeError, ValueError):
          print ("The file contents were unsuitable.")
      else:
          print ("The result was",result)
if __name__ == "__main__":
     getfilename()

My code’s output

Give the file name: hahaha...NO
Asked By: user3358884

||

Answers:

You’re calling getfilename in your __main__ block when you really want to call main.

To avoid the subsequent TypeError you’re receiving, you’ll want to coerce filetext into a number before using it in your division. Given your expected output is:

"The result was 3.194888178913738"

…you probably mean to make it a float rather than an int:

result = 1000 / float(filetext)
Answered By: Matthew Trevor
 filename = input("Give the file name: ")
    try:
        file = open(filename, "r")
        data = file.read().replace('n', '')
        divider = int(data)
        result = 1000 / divider
        file.close()
    except FileNotFoundError:
        print("There seems to be no file with that name.")
    except (TypeError, ValueError):
        print("The file contents were unsuitable.")
    except Exception:
        print("There was a problem with the program.")
    else:
        print("The result was", result)
Answered By: Angelina
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.