首页 > 解决方案 > if 语句在 Python 中工作错误

问题描述

我正在尝试“成功登录”,但它以“请输入您的新密码:”回复我。这是我的代码:

nvp=input("Please enter usernames and passwords: ")
nvp1= nvp.split(";")
nvp2=[]
names=[]
sd=[]
for x in nvp1 :
  nvp2.append(x)
  x= x.split(":")
  names.append(x[0])
  sd.append(x[1])
sd1=[]
for x in sd :
  sd1.append(x[:0:-1])
password=[]
c=0
while c != len(names) :
  n= names[c]
  n1=n[1:]
  p =sd1[c]
  p=p[len(n1):]
  password.append(p) 
  c += 1   
lm= input("Please enter last modification information: ")
lm= lm.split(";")
lmv=[]
for x in lm :
  c= 0
  c+=(int(x[x.index(":")+1])*10)
  c+=(int(x[x.index(" ")+1]))
  if (x[x.index(" ")+2]) == "0" : 
    c+=10 
  lmv.append(c)
un=input("Please enter the username: ")
if un in names:
  pw=input("Please enter the password: ")
  if pw in password :
    if lmv[names.index(un)] >= 6:
      p= input("Please enter your new password: ")
    else:
      print("Successful login")  
  else:
    print("Wrong password") 
else:
  print("Wrong username")  

我的样本是:

Please enter usernames and passwords: coff33c4ke:cr0t4id@lgoff33c4ke;j0mesb0nd7:jredips?etaraK0mesb0nd7;hubblebubble:h.sneil@_etucubblebubble

Please enter last modification information: hubblebubble:0year(s) 10month(s);coff33c4ke:2year(s) 5month(s);j0mesb0nd7:0year(s) 5month(s)

Please enter the username: j0mesb0nd7

Please enter the password: Karate?spider

Successful login

标签: pythonif-statement

解决方案


我无法发表评论,所以我会发表一篇文章,但很难理解你想要完成的事情。IE 什么是 lmv,你想用什么来完成if lmv[names.index(un)] >= 6:

如果您单步执行您的代码并插入一些打印语句,您将看到提供的输入信息

lmv = [11, 25, 5]
names.index(un) = 1
lmv[names.index(un)] = 25

因此if lmv[names.index(un)] >= 6:是正确的,并会提示您输入新密码。

也许您需要调整该逻辑以计算 lmv


推荐阅读