Python Forum
Exception with raise and input - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Exception with raise and input (/thread-33400.html)



Exception with raise and input - artemisellada - Apr-22-2021

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


RE: Exception with raise and input - Yoriz - Apr-22-2021

See the python docs User-defined Exceptions to find out how to create your own exceptions.


RE: Exception with raise and input - ndc85430 - Apr-23-2021

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?


RE: Exception with raise and input - jefsummers - Apr-23-2021

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...