How to determine integer in python

coldplay.xixi
Release: 2021-03-05 16:21:27
Original
22696 people have browsed it

Python method to determine integers: 1. You can use the is digit method of string str to determine whether the string is composed of only digits; 2. You can use the [try-except] statement to capture ValueError and continue the request enter.

How to determine integer in python

The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer.

Python method to determine integers:

1. You can use the is digit method of string str to determine whether the string is composed of only digits, or It's an integer. If it is an integer, exit the while loop, otherwise continue to request input.

while True: x = input('Input an integer: ') if x.isdigit(): break else: print 'Please input an *integer*'
Copy after login

2. You can also use thetry-exceptstatement. If the input string is an integer, then it can be converted to the int class using the int() function and exit the loop. Otherwise, a ValueError will occur. You can use the try-except statement to capture the ValueError and then continue to request input.

while True: try: x = input('Input an integer: ') x = int(x) break except ValueError: print 'Please input an *integer*'
Copy after login

Related free learning recommendations:python video tutorial

The above is the detailed content of How to determine integer in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!