Python Forum

Full Version: Exception with raise and input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I have a question about an assignment in my programming course Confused
I need to create an exception so that only integer numbers can be accepted

raise should be included

This is what I have until now...

n = int((input("Eingabe: ")))

if n != int:
    raise ValueError("Please enter an integer")
I don't necessarily want a ready-made solution, preferably an explanation of how I get my solution

Thank you very much ! Blush
See the python docs User-defined Exceptions to find out how to create your own exceptions.
Your code doesn't make sense - you're checking that n isn't equal to the int function, which will always be True there. As a hint on how to do it correctly: what does int do if you give it something that can't be converted to an integer?
Consider - if you don't try to convert to an int in the input line, could you try to see if the input value and the integer part of the input value are the same? If not, then raise an exception?

Since raise is a requirement of the assignment...