[英]String Index out of range python?
我正在用python創建一個安全登錄程序,並且不斷收到此錯誤消息: IndexError: string index out of range.
錯誤是針對line character=password[c]
。 我正在檢查用戶輸入的密碼是否有數字。 如果沒有數字,則密碼無效。 我該如何解決?
while c<numbercharacters:
character=password[c]
c=c+1
while character!="1" and character!="2" and character!="3" and character!="4" and character!="5" and character!="6" and character!="7" and character!="8" and character!="9" and character!=0: #checking if password has a number
print "This password is not valid, it does not contain a number. Please create another."
password= raw_input ()
numbercharacters=len(password)
character=password[c]
c
是計數器的變量。
當您獲得新的密碼輸入時(由於無效),您無需將計數器c
重置為零即可再次從頭開始檢查過程。
# After this block
password= raw_input ()
numbercharacters=len(password)
# Reset your counter to zero
c = 0
character=password[c]
您也可以簡單地使用以下任一方法為字符檢查數字
while not character.isdigit():
# or
while character not in '0123456789':
如果要檢查是否有電話號碼,您可以做
password= raw_input ()
if not any(character.isdigit() for character in password):
print "This password is not valid, it does not contain a number. Please create another."
如果您想重新詢問密碼直到輸入密碼
password= raw_input ()
if not any(character.isdigit() for character in password):
while not any(character.isdigit() for character in password):
print "This password is not valid, it does not contain a number. Please create another."
password= raw_input ()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.