带If条件的Python密码输入
所以,只要学习python,if/else和input都有问题,任务很简单,think只是一个简单的解决方案,但已经被卡住了,下面是我的代码,但即使信息正确,也会一直打印无效。怎么了带If条件的Python密码输入,python,python-3.x,Python,Python 3.x,所以,只要学习python,if/else和input都有问题,任务很简单,think只是一个简单的解决方案,但已经被卡住了,下面是我的代码,但即使信息正确,也会一直打印无效。怎么了 user= input("Please enter your user") pw= input("Please enter Password") if user == "Lena" and pw == "123456": print("Successfully Logged In") else:
user= input("Please enter your user")
pw= input("Please enter Password")
if user == "Lena" and pw == "123456":
print("Successfully Logged In")
else:
print("Invalid Credentials")
您上面的代码似乎可以很好地实现您想要实现的目标 可以这样说,你在
Lena
中加了一个小写的L
如果希望用户名不区分大小写,请尝试以下操作:
user= input("Please enter your user\n")
pw= input("Please enter Password\n")
if user.lower() == "lena" and pw == "123456":
print("Successfully Logged In")
else:
print("Invalid Credentials")
Please enter your user
LENA
Please enter Password
123456
Successfully Logged In
.lower()
是一种字符串方法,它使字符串中的所有字母都小写。然后可以将其检查为小写字符串
您也可以使用
.upper()
获得类似效果。您是否尝试过打印调试消息或使用调试器?对我来说很好。输入时是否确保Lena
中的L
为大写?工作正常,至少是@quamranaWorking fine编辑的问题。请在输入数据时检查输入错误。您还可以添加print
语句来显示程序得到了什么。