2015-10-13 89 views
-3

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

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

这绝对是一个更好的解决方案,只是想不出任何。

+1

有什么问题* *你有什么是什么呢? *“这里应该是取消字符串inout的东西:)”* - 用户的输入是* always *一个字符串。 – jonrsharpe

+0

问题是,没有字符不允许输入,只是数字....我在python newb,但得到它解决。日Thnx。 – caubert

回答

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

Thnx!我不知道jonrsharpe有什么问题,但会尝试你的解决方案。 – caubert