The program below starts with 100 cookies. It then asks the user to enter a number of cookies to eat (1-100) and displays the number of remaining cookies. The program works great if the user enters a valid integer.

I have 100 cookies.
How many cookies would you like to eat (1-100)? 5
That will leave 95 cookies.
Hope you enjoy your cookies!

Your task is to add input validation to ensure the user enters an integer in the range 1 to 100. The program should loop while the numCookies is invalid, allowing the user to provide input. If the user enters an invalid integer like -1, the user should see an error message and the loop continues for another input. Similarly, if the user enters a value like "1.0" or "one" that can't be converted to an integer, the user should see an error message and another input prompt.

Add your own logic to meet the program requirements. We recommend these general steps:

1. Use a "while" loop to continue asking the user for input as long as numCookies is outside the allowed range (1-100).
2. Protect the risky input conversion with a try block.
3. Add "if" logic to see if the user entered an integer that is outside the range 1-100. If the input is valid, print the resulting number of cookies, otherwise print a "Please try again." error message.
4. Add an "except" block to print the "Please try again." error message if non-integer input causes an exception.

You don't need to change any of the existing statements, but you must indent some of them as needed to make them part of your new "while" loop and/or "try" block.

The following example demonstrates the expected program output.

I have 100 cookies.
How many cookies would you like to eat (1-100)? five
Please try again.
How many cookies would you like to eat (1-100)? -1
Please try again.
How many cookies would you like to eat (1-100)? 101
Please try again.
How many cookies would you like to eat (1-100)? 50
That will leave 50 cookies.
Hope you enjoy your cookies!

cookies = 100
print("I have", cookies, "cookies.")
numCookies = 0

while True:
try:
numCookies = int(input("How many cookies would you like to eat (1-100)? "))
if 1 <= numCookies <= 100:
break
else:
print("Please try again.")
except ValueError:
print("Please try again.")

print("That will leave", cookies - numCookies, "cookies.")
print("Hope you enjoy your cookies!")

cookies = 100

print("I have", cookies, "cookies.")
numCookies = 0

# Input validation loop
while True:
try:
numCookies = int(input("How many cookies would you like to eat (1-100)? "))
if 1 <= numCookies <= 100:
break
else:
print("Please try again.")
except ValueError:
print("Please try again.")

print("That will leave", cookies - numCookies, "cookies.")
print("Hope you enjoy your cookies!")