Python Forum
Exception with raise and input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exception with raise and input
#1
Smile 
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
Reply
#2
See the python docs User-defined Exceptions to find out how to create your own exceptions.
Reply
#3
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?
Reply
#4
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...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have 'Failed: DID NOT RAISE <class 'Exception'>' williamKrakos 3 652 Feb-20-2024, 09:46 PM
Last Post: williamKrakos
  Raise an exception for syntax error sbabu 8 3,151 Feb-10-2020, 01:57 AM
Last Post: sbabu
  raise error mcgrim 4 2,821 May-09-2019, 08:37 PM
Last Post: mcgrim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020