Try a Validation Loop¶

Run this starter code. Pretty simple.

def main():
    while True:
        try:
            age = int(input("Please enter your age: "))
            break
        except _____Error:
            print("Need to input an integer!\n")
    print(f"Wow, you are {age} years old.")


if __name__ == "__main__":
    main()

What you should do¶

  1. What is the error that happens when you try to convert a string that cannot be converted to an integer?

  2. Use that error in the except section.

  3. Purposefully enter invalid input to see how the program handles the error.

  4. Why doesn’t the loop break right after taking the input?

What it should look like¶

Please enter your age: hello there sonny!
Need to input an integer!

Please enter your age: hmm? could you speak in my good ear?
Need to input an integer!

Please enter your age: oh, sorry..
Need to input an integer!

Please enter your age: 105
Wow, you are 105 years old.

©2021 Daniel Gallo

This work is licensed under Attribution-NonCommercial-ShareAlike 4.0 International