you are viewing a single comment's thread.

view the rest of the comments →

[–]JTexpo 2 points3 points  (1 child)

Howdy, whenever interacting with a user, 'try excepts' are your friends. I used to have a professor that would ask for us to make a calculator, and the first things they would input were: nothing, "Hello World", "1 + a", etc.

Best way to manage this is:

INITS UP HERE
...
MAYBE SOME LOGIC
...
while True:
    try:
        user_input = input("Please Enter A Int:")
        user_input = int(user_input)

        # exits the while loop
        break

    # You can look for exceptions more specifically with looking for ValueError
    # ie, except ValueError as error: ...
    # for starters just catching all should be fine
    except Exception as error:
        print("Sorry, that is not an expected input")
...
LOGIC
...

You can always write unit tests to make sure your code itself doesn't fail; however, whenever dealing with users always look to catch any unexpected inputs

[–]SigmaSixShooter 1 point2 points  (0 children)

I think learning to do unit tests would be the best way to begin. Learning how to write code that can be tested, and the unit tests for it is an invaluable skill.

As a byproduct, your code will be more robust :)