elif没有使用python代码创建密码

Jam*_*len -1 python

我是python的新手.每当我使用" elif"时,它表示它是无效的语法.有人可以帮忙解决问题吗?

repeat = True
while repeat:
 password = input("Please enter a password: ")
 if (len(password)) <8:
       choice = input ("Your password needs to have at least 8 letters. 1 = Try Again, 2 = Exit: ")
       if choice == "2":
        repeat = False
 else:
    print ("That is a valid password.")
    exit
 elif not any(char.isdigit() for char in password):
      choice = input ("Your password needs to have at least 1 number in it. 1 = Try Again, 2 = Exit: ")
      if choice == "2":
       repeat = False
 else:
     print ("That is a valid password.")
     break
Run Code Online (Sandbox Code Playgroud)

Sam*_*aig 5

elif块需要紧跟if块或其他elif块.你的elif跟着一个别的阻拦.python中不允许这样做.修复它非常简单,只需删除第一个其他块:

repeat = True
while repeat:
 password = input("Please enter a password: ")
 if (len(password)) <8:
       choice = input ("That is not a valid password.\nYour password needs to have at least 8 letters. 1 = Try Again, 2 = Exit: ")
       if choice == "2":
        repeat = False
 elif not any(char.isdigit() for char in password):
      choice = input ("That is not a valid password.\nYour password needs to have at least 1 number in it. 1 = Try Again, 2 = Exit: ")
      if choice == "2":
       repeat = False
 else:
     print ("That is a valid password.")
     break
Run Code Online (Sandbox Code Playgroud)