Search code examples
pythonpython-3.xredditpraw

How to take different input types and do something different with each? Python 3.5


So I am trying to make a Reddit bot for personal use, as a learning project, and I am having trouble adding error exceptions for inputs.

Here is the entire source code: http://pastebin.com/DYiun1ux

The parts that are solely in question here are

 while True:
 if type(thing_limit) == int:
     print("That is a number, thanks.")
     break
 elif type(thing_limit) == float:
     print("You need a whole number.")
     sys.exit()
 elif type(thing_limit) == str:
     print("You need a whole number.")
     sys.exit()
 else:
     print("That is a number, thanks.")
     break

I'm not sure how I would go about making sure that the username that I put in is valid or not. Thanks!


Solution

  • input always returns a string. Your best choice is trying to convert the result to an integer.

    try:
        thing_limit = int(thing_limit)
    except ValueError:
        print("You need a whole number.")
        sys.exit()
    else:
        print("That is a number, thanks.")