英文:
how to keep the code running after log in (python 3)
问题
我正在尝试编写一个程序,允许用户创建登录信息,然后运行一个菜单系统。我遇到的问题是它一直在循环执行登录代码,而不是继续执行下一部分,即菜单部分。我尝试添加break命令,但似乎只停在登录后,而不运行下一部分。
# Aaron Dyer Software Engineering Fundamentals Data Base## #
# 提示用户登录#
print("请输入登录信息")
# 以下代码允许用户创建用户名和密码并登录
def add_new_user():
username = input("输入用户名:")
password = input("输入密码:")
user_type = input("输入用户类型(管理员、库存检查员):")
with open("userdata.txt", "a") as f:
f.write("%s %s %s\n" % (username, password, user_type))
def user_authentication():
username = input("输入用户名:")
password = input("输入密码:")
with open("userdata.txt", "r") as f:
for line in f:
user_data = line.split()
if user_data[0] == username and user_data[1] == password:
print("登录成功!")
return user_data[2]
print("密码或用户名不正确。")
return None
def main():
add_new_user()
while True:
user_type = user_authentication()
if user_type is None:
continue
if user_type == "admin":
print("您已登录为管理员。")
continue
elif user_type == "inventory-checker":
continue
print("您已登录为库存检查员。")
if __name__ == "__main__":
main()
# 下面是用于访问笔记本电脑和iPhone详细信息的菜单系统#
def menu():
print("[1] 笔记本电脑")
print("[2] iPhones")
option = input("选择您的设备:")
if option == "1":
print("[1] S5750")
print("[2] S5577")
print("[3] S5563")
print("[4] S5744")
print("[5] S5765")
print("[6] S5944")
print("[7] S5507")
print("[8] S5509")
laptop_option = input("选择您的笔记本电脑:")
# 根据选择的笔记本电脑执行操作
elif option == "2":
# 在这里添加iPhone选项
pass
else:
print("选择无效。请重试。")
英文:
i am trying to write a programe that lets the user create a log in and then it runs a menu system. the problem i have is it keeps looping the log in code instead of moving onto the next part which is the menu bit. i have tried adding break commands but this seemed to stop after the log in but not run the next part.
# Aaron Dyer Software Engineering Fundamentals Data Base## #
# A Prompt asking the user to log in#
print("Please enter log in details")
# The below code will allow the user to create a username and password and log in"
def add_new_user():
username = input("Enter your username: ")
password = input("Enter your password: ")
user_type = input("Enter your user type (admin, inventory-checker): ")
with open("userdata.txt", "a") as f:
f.write("%s %s %s\n" % (username, password, user_type))
def user_authentication():
username = input("Enter your username: ")
password = input("Enter your password: ")
with open("userdata.txt", "r") as f:
for line in f:
user_data = line.split()
if user_data[0] == username and user_data[1] == password:
print("Login successful!")
return user_data[2]
print("Incorrect password or username.")
return None
def main():
add_new_user()
while True:
user_type = user_authentication()
if user_type is None:
continue
if user_type == "admin":
print("You are logged in as an admin.")
continue
elif user_type == "inventory-checker":
continue
print("You are logged in as an inventory checker.")
if __name__ == "__main__":
main()
#Below is the menu stystem to access the detials about the laptops and iphone's#
def menu():
print("[1] Laptops")
print("[2] iPhones")
option = input("Choose your device: ")
if option == "1":
print("[1] S5750")
print("[2] S5577")
print("[3] S5563")
print("[4] S5744")
print("[5] S5765")
print("[6] S5944")
print("[7] S5507")
print("[8] S5509")
laptop_option = input("Choose your laptop: ")
# do something based on the chosen laptop
elif option == "2":
# add options for iPhones here
pass
else:
print("Invalid choice. Please try again.")
答案1
得分: 1
只需将菜单功能放在main之前。在检查是否是管理员或库存检查员之后,只需转到菜单功能。
这是您编辑后的代码:
# Aaron Dyer Software Engineering Fundamentals Data Base## #
# 提示用户登录#
print("请输入登录详情")
# 以下代码允许用户创建用户名和密码并登录#
def add_new_user():
username = input("输入您的用户名:")
password = input("输入您的密码:")
user_type = input("输入您的用户类型(admin、inventory-checker):")
with open("userdata.txt", "a") as f:
f.write("%s %s %s\n" % (username, password, user_type))
def user_authentication():
username = input("输入您的用户名:")
password = input("输入您的密码:")
with open("userdata.txt", "r") as f:
for line in f:
user_data = line.split()
if user_data[0] == username and user_data[1] == password:
print("登录成功!")
return user_data[2]
print("密码或用户名不正确。")
return None
def menu():
print("[1] 笔记本电脑")
print("[2] iPhones")
option = input("选择您的设备:")
if option == "1":
print("[1] S5750")
print("[2] S5577")
print("[3] S5563")
print("[4] S5744")
print("[5] S5765")
print("[6] S5944")
print("[7] S5507")
print("[8] S5509")
laptop_option = input("选择您的笔记本电脑:")
# 根据所选笔记本电脑执行某些操作
elif option == "2":
# 在这里添加iPhone的选项
pass
else:
print("无效的选择,请重试。")
def main():
add_new_user()
while True:
user_type = user_authentication()
if user_type is None:
continue
if user_type == "admin":
print("您已登录为管理员。")
menu()
continue
elif user_type == "inventory-checker":
menu()
print("您已登录为库存检查员。")
if __name__ == "__main__":
main()
#以下是访问笔记本电脑和iPhone详细信息的菜单系统#
请注意,我已经删除了HTML实体代码,以便更清晰地查看Python代码。
英文:
You just need to put the menu function before main .
and just go to menu function after check that is admin or inventory checker .
that is your code after editing :
# Aaron Dyer Software Engineering Fundamentals Data Base## #
# A Prompt asking the user to log in#
print("Please enter log in details")
# The below code will allow the user to create a username and password and log in"
def add_new_user():
username = input("Enter your username: ")
password = input("Enter your password: ")
user_type = input("Enter your user type (admin, inventory-checker): ")
with open("userdata.txt", "a") as f:
f.write("%s %s %s\n" % (username, password, user_type))
def user_authentication():
username = input("Enter your username: ")
password = input("Enter your password: ")
with open("userdata.txt", "r") as f:
for line in f:
user_data = line.split()
if user_data[0] == username and user_data[1] == password:
print("Login successful!")
return user_data[2]
print("Incorrect password or username.")
return None
def menu():
print("[1] Laptops")
print("[2] iPhones")
option = input("Choose your device: ")
if option == "1":
print("[1] S5750")
print("[2] S5577")
print("[3] S5563")
print("[4] S5744")
print("[5] S5765")
print("[6] S5944")
print("[7] S5507")
print("[8] S5509")
laptop_option = input("Choose your laptop: ")
# do something based on the chosen laptop
elif option == "2":
# add options for iPhones here
pass
else:
print("Invalid choice. Please try again.")
def main():
add_new_user()
while True:
user_type = user_authentication()
if user_type is None:
continue
if user_type == "admin":
print("You are logged in as an admin.")
menu()
continue
elif user_type == "inventory-checker":
menu()
print("You are logged in as an inventory checker.")
if __name__ == "__main__":
main()
#Below is the menu stystem to access the detials about the laptops and iphone's#
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论