Python 3.x输入变量

时间:2015-10-13 09:09:24

标签: python python-3.x

我希望用户输入的代码长度必须为11个数字且不得包含任何字符。否则,将重复输入的问题。

coalesce(B.Price, 0) as Price 

对此有一个更好的解决方案,只是想不到任何。

1 个答案:

答案 0 :(得分:0)

这可能是你之后的

def validate(s):
    for char in s: #iterate through string
        if not char.isdigit(): # checks if character is not a number
            return False # if it's not a number then return False
    return True # if the string passes with no issues return True

def enter_code():
    while True: #Will keep running until break is reached
        code = input("Please enter your code: ") #get user input
# if the length of input and it passes the validate then print 'Your code is valid'
        if len(code) == 11 and validate(code):
            print('Your code is valid')
            break #break out of loop if valid
# if code fails test then print 'Your code is invalid' and return to start of while loop
        print('Your code is invalid')

if __name__ == '__main__':
    enter_code()