python学习day3
Posted dingyang-python
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习day3相关的知识,希望对你有一定的参考价值。
1丶 用户先进行登陆如果用户名在文件中且用户密码也正确就登陆成功调用购物车函数,如果用户用户名输入正确密码错误,提示用户密码错误且重新输入,如果用户
输入用户名不存在,提示用户是否创建该用户,调用注册函数。
1.1、用户登录完成后,要求其输入初始金额,如果用户名或密码输入错误超过三次则退出程序。
2、 显示当前的主菜单列表 ["商品列表", "购物车", "充值"] 定义print_main_menu函数呈现主菜单
2.1、 商品列表: 定义一个商品字典 [{‘name‘:‘电脑‘,‘price‘:5000},{},{},...] 定义print_commodity_list函数呈现商品列表
2.2、 购物车: 用户购买的商品,形成一个列表呈现,以及金额的total, [{‘name‘:‘电脑‘,‘price‘:5000},{},{},] 定义print_shopping_car函数呈现购物车
2.3、 结算: 结算功能在购物车里,向用户提供结算功能,用户选择后 用户的初始金额 - 购物车中的总金额, 包括判断 "余额不足"
2.4、 充值: 当用户选择充值时,要求用户输入一个非负值,与用户的余额进行相加操作
# _*_ coding:utf-8 _*_
"""
解题思路
创建用户登录函数,用户登错3次程序退出
创建新用户注册函数(如果用户用户名没在用户文件中提示是否新建用户)
创建购物车函数
"""
import time
def login():
flag = False
count = 0
while True:
user_name = input("请输入用户名:")
password = input("请输入密码:")
# user_name = "dingyang"
# password = "12"
with open("login", encoding="utf-8", mode="r") as f:
for countent in f:
line = countent.strip().split()
#print(line)
if count == 2:
print("您已经输错三次,已经不能再输入了。")
exit()
if user_name == line[0] and password == line[1]:
flag = True
break
if flag :
print("欢迎%s登陆成功" % user_name)
shopping_car()
break
else:
with open("login", encoding="utf-8", mode="r") as f1:
for counten in f1:
line1 = counten.strip().split()
# print(line1)
if user_name != line1[0]:
#print(line1[0])
# while True:
print("您输入的用户名不存在,需要注册%s吗?" % user_name)
user_input = input("按y进行注册,按q重新输入:")
if user_input == "y":
print(user_name)
user_register(user_name)
elif user_input == "q":
break
else:
print("您输入有误,请输入y或q")
else:
print("%s用户密码错误,请重新输入!" % user_name)
break
count = count + 1
def user_register(username):
while True:
register_passwd = input("请输入新用户密码:")
new_register_passwd = input("请再次输入密码: ")
if register_passwd == new_register_passwd:
with open("login",encoding="utf-8",mode="a") as f2:
f2.write("\n%s " % username)
f2.write(new_register_passwd)
f2.close()
print("用户%s注册成功" % username)
login()
break
else:
print("您输入的密码不一致,请重新输入:")
def shopping_car():
print("欢迎使用淘淘宝".center(50, "-"))
user_rmb()
userRMB = 0
def user_rmb():
while True:
try:
userRMB = int(input("请输入金额数(10-10000):"))
except ValueError as e:
print("输入非法,请重新输入")
continue
if userRMB < 10 or userRMB > 10000:
print("输入非法,请重新输入")
else:
break
main_Menu = ["商品列表", "查看购物车", "充值"]
def print_main_menu():
‘‘‘主菜单呈现‘‘‘
global main_Menu
for index,item in enumerate(main_Menu):
menu_index = index + 1
print("%d. %s" % (menu_index,item))
commodity_List = [
{"name":"ThinkPad","price":3500},
{"name":"Iphone","price":4500},
{"name":"Three Squirrels","price":30},
{"name":"Ipod","price":500}
]
def print_commodity_list():
‘‘‘商品列表呈现‘‘‘
global commodity_List
print("商品列表".center(50, "-"))
for index, item in enumerate(commodity_List):
commodity_index = index + 1
print(str(commodity_index).center(10, " "), str(item["name"].ljust(31, " ")), str(item["price"]))
def print_shopping_cart():
‘‘‘购物车列表呈现‘‘‘
global shopping_cart
print("购物车列表".center(50, "-"))
for index, item in enumerate(shopping_cart):
shopping_cart_index = index + 1
print(str(shopping_cart_index).center(10, " "), str(item["name"].ljust(31, " ")), str(item["price"]))
shopping_cart = []
#C =
login()
while True:
print_main_menu()
try:
menu_choice = int(input("请输入要选择的操作:"))
except ValueError as e:
print("输入非法,请重新输入")
continue
if menu_choice < 1 or menu_choice > len(main_Menu):
print("输入非法,请重新输入")
if main_Menu[menu_choice - 1] == "商品列表":
while True:
print_commodity_list()
try:
commodity_choice = int(input("请选择要购买的商品:"))
except ValueError as e:
print("输入非法,请重新输入")
continue
if commodity_choice < 1 or commodity_choice > len(commodity_List):
print("输入非法,请重新输入")
continue
shopping_cart.append(commodity_List[commodity_choice - 1])
print("添加购物车成功!")
user_operation = input("继续:y\n退出:e\n返回上一级:q ")
if user_operation == ‘y‘:
continue
elif user_operation == ‘e‘:
exit(0);
elif user_operation == ‘q‘:
break
else:
print("输入非法,请重新输入")
elif main_Menu[menu_choice - 1] == "查看购物车":
while True:
total_consume = 0
print_shopping_cart()
for i in shopping_cart:
total_consume += i[‘price‘]
print("所有商品总金额为:", total_consume)
user_operation = input("立即结算:c\n退出:e\n返回上一级:q ")
if user_operation == ‘c‘:
if userRMB > total_consume:
userRMB -= total_consume
print("结算成功:本次消费 %d,您的账户余额为 %d" % (total_consume, userRMB))
shopping_cart = []
else:
print("您的账户余额不足,请返回上一级充值后,再进行支付.")
elif user_operation == ‘e‘:
exit(0);
elif user_operation == ‘q‘:
break
else:
print("输入非法,请重新输入")
elif main_Menu[menu_choice - 1] == "充值":
while True:
try:
recharge_amount = int(input("请输入要充值的金额数,每次充值不得小于100"))
except ValueError as e:
print("输入非法,请重新输入")
if recharge_amount < 100:
print("输入金额过少,充值金额不得小于100")
continue
userRMB += recharge_amount
print("恭喜你充值成功,本次充值金额为 %d, 余额为 %d, 3秒后返回主菜单" % (recharge_amount, userRMB))
time.sleep(3)
break
以上是关于python学习day3的主要内容,如果未能解决你的问题,请参考以下文章