写一个简单的模拟登陆python程序
模拟登录一:
a = 'zhangsan'
b = '123456'
def system():
if name == a and password == b:
return '欢迎登入系统!'
elif name != a and password == b:
return '用户名不正确,请重输!'
elif name == a and password != b:
return '用户密码不正确,请重输!'
else:
return '账号密码均错误,请重新输入!'
name = input('请输入用户名:')
password = input('请输入用户密码')
print(system())
模拟登录二:
user_name = 'Alex'
user_password = '123456'
def user_login(name, password):
if name == user_name and password == user_password:
return '欢迎登入系统!'
elif name != user_name and password == user_password:
return '用户名不正确,请重输!'
elif name == user_name and password != user_password:
return '用户密码不正确,请重输!'
else:
return '账号密码均错误,请重新输入!'
name1 = input('请输入用户名:')
password1 = input('请输入用户密码')
info = user_login(name1, password1)
print(info) # info信息,print(info)其实就是调用函数
模拟登录三(使用函数):
user_info = ("Alex", "abc123") # 设好账号密码
def check_id_password(id, password):
# 定义函数 检查账号密码
if (user_info[0] == id) and (user_info[1] == password):
return 0
elif (user_info[0] == id) and (user_info[1] != password):
return 1
elif (user_info[0] != id) and (user_info[1] == password):
return 2
elif (user_info[0] != id) and (user_info[1] != password):
return 3
for i in range(3):
user_id = input("please input your id:")
user_password = input("please input your password:")
# 调用函数,把input的量变量作为参数传入id和password
return_info = check_id_password(user_id, user_password)
if return_info == 0:
print('登录成功!')
break
elif return_info == 1:
print('第' + str(i + 1) + '次猜错,密码错误,还有' + str(2 - i) + '次机会')
elif return_info == 2:
print('第' + str(i + 1) + '次猜错,账号错误,还有' + str(2 - i) + '次机会')
elif return_info == 3:
print('第' + str(i + 1) + '次猜错,账号密码都有错误,还有' + str(2 - i) + '次机会')
else:
print("猜错三次,不能继续!")
模拟登录四(使用while):
user_info = ("jack", "abc123") # 设置账号密码信息
i = 0 # 对循环体进行初始化
while i < 3: # 设定while循环
user_id = input("please input your id:")
user_password = input("please input your password:")
if (user_info[0] == user_id) and (user_info[1] == user_password):
# 第一种情况:账号密码都对
print("登录成功")
break
elif (user_info[0] == user_id) and (user_info[1] != user_password):
# 第二种情况:账号正确,密码错误
print("第" + str(i + 1) + "次猜错,密码错误,还有" + str(2 - i) + "次机会")
elif (user_info[0] != user_id) and (user_info[1] == user_password):
# 第三种情况:账号错误,密码正确
print("第" + str(i + 1) + "次猜错,账号错误,还有" + str(2 - i) + "次机会")
elif (user_info[0] != user_id) and (user_info[1] != user_password):
# 第四种情况:账号密码都错
print("第" + str(i + 1) + "次猜错,账号密码都错,还有" + str(2 - i) + "次机会")
i = i + 1 # 在循环体内部通过语句语句更改循环变量的值,否则会陷入死循环
else:
print("已猜三次,不能继续")
模拟登录五(使用for):
user_info = ("jack", "abc123") # 设置账号密码信息
for i in range(3):
user_id = input("please input your id:")
user_password = input("please input your password:")
if (user_info[0] == user_id) and (user_info[1] == user_password):
# 第一种情况:账号密码都对
print("恭喜,登录成功!")
break
elif (user_info[0] == user_id) and (user_info[1] != user_password):
# 第二种情况:账号正确,密码错误
print("第" + str(i + 1) + "次猜错,密码错误,还有" + str(2 - i) + "次机会")
elif (user_info[0] != user_id) and (user_info[1] == user_password):
# 第三种情况:账号错误,密码正确
print("第" + str(i + 1) + "次猜错,账号错误,还有" + str(2 - i) + "次机会")
elif (user_info[0] != user_id) and (user_info[1] != user_password):
# 第四种情况:账号密码都错
print("第" + str(i + 1) + "次猜错,账号密码都错,还有" + str(2 - i) + "次机会")
else:
print("猜错三次,不能继续!")
|