[sf-lug] ThinkPython: coding problem

Alex Kleider a_kleider at yahoo.com
Fri Oct 10 21:42:27 PDT 2008


# ThinkPython: question-

As part of our ThinkPython class exercises I want to 
check user input.
i.e. the user is asked to enter a number and I want the routine 
to check that it is indeed a number that is entered.
It seems that everything "collected" by raw_input is considered 
a string, even if only digits are included.


  
def int_check(n):
  if type(n)==int:
    return True   # as used below, NEVER returns True.
  else:
    return False

response=raw_input('enter a number: ')
if int_check(response):
  pass #procede with what needs to be done 
else:
  pass #take corrective action

Can anyone suggest how I can accomplish this?
I'm familiar with Pascal and its strong typing which would prevent such a problem from occurring as a runtime problem (vs a syntactic error.)

ak
~ 


      




More information about the sf-lug mailing list