简体   繁体   中英

Python: Any ideas on how to check is an element in a string is a digit or not, without using .isdigit()?

I am supposed to write a code for a program that will ask a user to input an integer and if the input is not an integer it will print 'try again' and if it is an integer it just quits the program. The only issue is that we are not allowed to use the .isdigit() method. I need to come up with a method to check if each element in a strong is a digit or not without using .isdigit() .

while True:
    input_str = raw_input('input: ')
    input_set = set(input_str)
    digit_set = set([str(digit) for digit in range(0, 10)])
    # print "isdigit: ", digit_set.issuperset(input_set)
    if digit_set.issuperset(input_set) is True:
        break
    print 'try again'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM