Python input validation for both integer and string

前端 未结 1 1135
清酒与你
清酒与你 2021-01-13 12:59

I\'m new in programming and I have an issue when doing the input validation. My program requires to input number from 1 to 10 or the letter y but it seems that

相关标签:
1条回答
  • 2021-01-13 13:36

    as jamylak suggested change the if condition to :

    if a == 'y' or 1 <= int(a) <= 10:
    

    program:

    def checkingInput():
        while True:
            try:
                a = input('enter')
                if a == 'y' or 1 <= int(a) <= 10:
                    return a
                else:
                    print('Invalid input!')
            except ValueError:
                print('Value error! Please try again!')
    
    0 讨论(0)
提交回复
热议问题