使用 Python 实现 if-else 条件
Implementing if-else conditionals with Python
我必须登录,但由于某种原因无法登录。我能帮忙吗?
我有这个代码,但我无法让它工作。
username=input("please enter your username")
password=input("please enter your password")
if username=="student1":
password=="password123"
print("accsess granted")
else username!="student1":
password !="password123"
print "inncorect login"
if username=="student1" and password=="password123":
print("accsess granted")
你的缩进不对
您的 if
格式不正确
你的自相矛盾的 print
陈述让人怀疑你使用的是什么版本(版本很重要!括号很重要!)
幸运的是,修复非常简单。您需要一个 if-else
语句。 else
不需要条件。
username = input("please enter your username")
password = input("please enter your password")
if username == "student1" and password == "password123":
print("access granted")
else:
print("incorrect login")
如果您使用的是 python2,请改用 raw_input
。
您 if/else 的语法有误。正确的语法是:
if username == "student1" and password == "password123":
print("access granted")
else:
print("incorrect login")
现在您的脚本仅检查用户名是否为 "student1" 并且对密码执行无用的检查。试试这个版本(假设 Python 2.7):
username = raw_input("please enter your username")
password = raw_input("please enter your password")
if username == "student1" and password == "password123":
print "access granted"
else:
print "incorrect login"
更好的是,您应该散列密码,因为现在打开 python 文件并环顾四周以找到正确的密码就足够了。例如:
from hashlib import md5
username = raw_input("please enter your username")
password = raw_input("please enter your password")
password2 = md5()
password2.update(password)
if username == "student1" and password2.hexdigest() == "482c811da5d5b4bc6d497ffa98491e38":
print "access granted"
else:
print "incorrect login"
我用这段代码生成了哈希:
from hashlib import md5
m = md5()
m.update('password123')
print m.hexdigest()
我必须登录,但由于某种原因无法登录。我能帮忙吗?
我有这个代码,但我无法让它工作。
username=input("please enter your username")
password=input("please enter your password")
if username=="student1":
password=="password123"
print("accsess granted")
else username!="student1":
password !="password123"
print "inncorect login"
if username=="student1" and password=="password123":
print("accsess granted")
你的缩进不对
您的
if
格式不正确你的自相矛盾的
print
陈述让人怀疑你使用的是什么版本(版本很重要!括号很重要!)
幸运的是,修复非常简单。您需要一个 if-else
语句。 else
不需要条件。
username = input("please enter your username")
password = input("please enter your password")
if username == "student1" and password == "password123":
print("access granted")
else:
print("incorrect login")
如果您使用的是 python2,请改用 raw_input
。
您 if/else 的语法有误。正确的语法是:
if username == "student1" and password == "password123":
print("access granted")
else:
print("incorrect login")
现在您的脚本仅检查用户名是否为 "student1" 并且对密码执行无用的检查。试试这个版本(假设 Python 2.7):
username = raw_input("please enter your username")
password = raw_input("please enter your password")
if username == "student1" and password == "password123":
print "access granted"
else:
print "incorrect login"
更好的是,您应该散列密码,因为现在打开 python 文件并环顾四周以找到正确的密码就足够了。例如:
from hashlib import md5
username = raw_input("please enter your username")
password = raw_input("please enter your password")
password2 = md5()
password2.update(password)
if username == "student1" and password2.hexdigest() == "482c811da5d5b4bc6d497ffa98491e38":
print "access granted"
else:
print "incorrect login"
我用这段代码生成了哈希:
from hashlib import md5
m = md5()
m.update('password123')
print m.hexdigest()