#Python - I have a question about entering variables!

Asked 1 years ago, Updated 1 years ago, 81 views

An error occurs when entering a string value instead of an integer value in age=int(input())

At this time, do you know the source code of the algorithm that uses conditional statements to output "Not an appropriate input type" to return to the initial stage?

python input

2022-09-21 23:04

1 Answers

You can use try/exception.

For example, you can specify exceptions in the exception as shown below, and if you are not sure what exceptions occur, you can catch all exceptions by writing exception:.

try:
    age=int (input ("Please enter your age")
except ValueError:
    print ("Please enter a number only")

In addition, you can check various exceptions by searching with python exception.


2022-09-21 23:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.