2015-10-13 119 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