Python3.x输入变量

Python3.x输入变量,python,python-3.x,Python,Python 3.x,我想获得一个代码的用户输入,该代码必须是11个数字,并且不能包含任何字符。否则,将重复要输入的问题 code=input("Please enter your code ") while len((code)) !=11: #here should be something to nullify strings inout :) code = input("Please enter your code and in numbers only ") 肯定有更好的解决办法,只是想不出任何办

我想获得一个代码的用户输入,该代码必须是11个数字,并且不能包含任何字符。否则,将重复要输入的问题

code=input("Please enter your code ")
while len((code)) !=11: #here should be something to nullify strings inout :)
    code = input("Please enter your code and in numbers only ")

肯定有更好的解决办法,只是想不出任何办法

这可能就是你想要的

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()

你有什么问题吗?这里应该有一些内容可以使inout中的字符串无效:-用户的输入始终是一个字符串。问题是,不允许输入任何字符,只允许输入数字。。。。我是python的新手,但已经解决了。Thnx,Thnx!我不知道jonrsharpe出了什么问题,但我会尝试你的解决方案。