Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

def is_digit(x):
    if type(x) == int:

        return True
    else:
        return False


def main():
    shape_opt = input('Enter input >> ')
    while not is_digit(shape_opt):
        shape_opt = input('Enter input >> ')
        
    else:
        print('it work')


if __name__ == '__main__':
    main()

So when the user input a value that is not an integer, the system will repeat the input(). Else, it does something else. But it won't work, may I know why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
145 views
Welcome To Ask or Share your Answers For Others

1 Answer

Check this. Input always returns a string. So isdigit() is better to use here. It returns True if all characters in a string are digits and False otherwise.

return x.isdigit() will evaluate to True/False accordingly, which will be returned

def is_digit(x):
    return x.isdigit()
    
def main():
    shape_opt = input('Enter input >> ')
    while not is_digit(shape_opt):
        shape_opt = input('Enter input >> ')
        
    else:
        print('it work')    

if __name__ == '__main__':
    main()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...