input() with int function

  here's an example of using input() with the int() function to get an integer input from the user:

python
age = int(input("How old are you? ")) 
print("You will be " + str(age+1) + " years old next year.")

In this example, we're using the input() function to prompt the user to enter their age, which we convert to an integer using the int() function. We then use this integer value to perform a simple calculation and print a message to the user.

Note that when we use the int() function to convert the user's input to an integer, we need to make sure that the input is actually a valid integer value, otherwise a ValueError will be raised. It's generally a good idea to handle this case gracefully by catching the exception and prompting the user to enter a valid value.

Post a Comment

Previous Post Next Post