Guest User

Untitled

a guest
Jun 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. def checkInput():
  2.     number = input("Give me an int (above 0)! ")        #checks if the input given is the same type
  3.     if (type(number) == type(0)) and (number > 0):      #as if the number was inputted, and in the
  4.         return number                   #same line checks if the input is greater than
  5.     else:                           #zero. If the first condition fails it doesn't
  6.         checkInput()                    #evaluate the second check because and
  7.                                 #requires truth in both
  8. a = checkInput()
  9.  
  10. #If you want something like this to use raw_input() and check if they entered a string then do something like this
  11. '''
  12. takes in raw input which is in the form of a string. TRIES to convert it to a integer, if it fails it skips to the except, which pretends the try never happened and continues along in the program
  13.  
  14. checks if the type of input is still a string incase any funny businness is going on and then makes the user change their input by calling the function again
  15.  
  16. the second if checks if it is above 0, this should only be gotten to if it passes the non string test and that means it probably pass the try
  17. '''
  18. def checkRawInput():
  19.     usrInput = raw_input("Give me an int (above 0)! ")
  20.     try:
  21.         usrInput = int(usrInput)
  22.     except:
  23.         pass
  24.     if type(usrInput) == type("dummy"):
  25.         print "please type in a number, not a word"
  26.         checkRawInput()
  27.     if usrInput > 0:
  28.         return usrInput
  29.     else:
  30.         "You did something really funny try again"     
  31.         checkRawInput()
Add Comment
Please, Sign In to add comment