Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def checkInput():
- number = input("Give me an int (above 0)! ") #checks if the input given is the same type
- if (type(number) == type(0)) and (number > 0): #as if the number was inputted, and in the
- return number #same line checks if the input is greater than
- else: #zero. If the first condition fails it doesn't
- checkInput() #evaluate the second check because and
- #requires truth in both
- a = checkInput()
- #If you want something like this to use raw_input() and check if they entered a string then do something like this
- '''
- 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
- 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
- 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
- '''
- def checkRawInput():
- usrInput = raw_input("Give me an int (above 0)! ")
- try:
- usrInput = int(usrInput)
- except:
- pass
- if type(usrInput) == type("dummy"):
- print "please type in a number, not a word"
- checkRawInput()
- if usrInput > 0:
- return usrInput
- else:
- "You did something really funny try again"
- checkRawInput()
Add Comment
Please, Sign In to add comment