3

I used:

day = int(input('Please input the day you were born: e.g 8th=8 21st = 21 : '))
month = int(input('Please input the month you were born: e.g may = 5 december = 12 : '))
year = int(input('Please input the year you were born: e.g 2001 / 1961 : '))

if day == int and month == int and year == int:

But it always even when it's an integer says it's wrong.

6
  • 1
    type(day) is int etc. Commented Nov 12, 2015 at 20:22
  • 3
    @axiom You should really use isinstance. Commented Nov 12, 2015 at 20:23
  • 1
    Are you using Python2 or Python3? Commented Nov 12, 2015 at 20:23
  • 7
    By virtue of wrapping your input calls in int(, your type checking becomes useless, because there are only two possibilities: 1) day, month, and year are all integers; or 2) your program crashed with a ValueError within the first 3 lines and the if will never run. Commented Nov 12, 2015 at 20:24
  • 1
    Could you give more detail about what "But it always even when it's an integer says it's wrong."? What exactly is your wrong input and output? Commented Nov 12, 2015 at 20:30

3 Answers 3

9
def get_int(p,error_msg="Thats Not An Int!"):
    while True:
         try:
            return int(raw_input(p))
         except (ValueError,TypeError):
            print "ERROR: %s"%error_msg

day = get_int('Please input the day you were born: e.g 8th=8 21st = 21 : ')
#day is guaranteed to be an int

I like to take this and abstract it further

 def force_type(type_class,prompt,error_msg):
     while True:
         try:
            return type_class(prompt)
         except (ValueError,TypeError):
            print error_msg

then it simply becomes

 def get_int(p,err_msg):
     return force_type(int,p,err_msg)
 def get_float(p,err_msg):
     return force_type(float,p,err_msg)
 ...

allthat said if you want to typecheck you should ~not~ use type(var) you should use isinstance(var,int)

Sign up to request clarification or add additional context in comments.

6 Comments

This isn't OOP enough. Can you make it more OOPy?
@MorganThrapp sure def get_int(self,p,err_msg): :P
is it possible for you to explain this to me
@benjiboi79 get_int tries to give you back the user's input (after casting to int). If it fails for whatever reason (like the user types something that isn't a number), it prints an error message and prompts the user to re-input.
@benjiboi79 the important thing is to realize that after you type int(...), the result WILL BE AN int. You don't have to test for that again. If it fails to convert it to int, it will raise an exception (ValueError or maybe TypeError) and stop execution of your program. By the time your code gets to where you're testing if they're ints, they're guaranteed to be ints.
|
1

To check type you can do:

type(aVar) is aType

Anyway, as Kevin said in a comment you're already wrapping input to int, so either it's actually an int or your program crashed

Comments

0

try

if type(day) == int and type(month) == int and type(year) == int

3 Comments

But that will be a tautology. Given the rest of his code, type(day) will always be int whenever that line of code is executed.
yeah, but i just wanted to show how to access type of variable. dont know why he has declared all three variables as int, in which case there is no need to compare!
you're welcome! please upvote/accept the answer if you found it useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.