python学习day3

news/2024/7/3 1:37:01

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















转载于:https://www.cnblogs.com/dingyang-python/p/9087349.html


http://lihuaxi.xjx100.cn/news/238070.html

相关文章

以太坊交易事件,日志的理解

想知道更多关于区块链技术知识&#xff0c;请百度【链客区块链技术问答社区】 链客&#xff0c;有问必答&#xff01;Ethereum transation event and log 以太坊交易事件的功能有三个&#xff1a; 用于返回智能合约执行过程中的返回值到用户界面同步触发前端用户界面事件便宜的…

Phpcms V9手机门户设置教程:怎么用PC V9做手机网站

一、在PHPcms V9管理后台设置手机门户 1.1、开启手机网站。位置&#xff1a;模块 》手机门户 》 添加手机站点&#xff0c;具体设置可参照截图&#xff1a; 填写站点名和LOGO文件相对位置&#xff0c;绑定用于手机网站的二级域名m.cmsyou.com&#xff0c;域名以http://开头。 1…

中年妇女xxx_2019年国际妇女节庆祝活动

中年妇女xxxBehind the scenes幕后花絮 Becoming Women Techmakers Ambassador成为女性技术制造者大使 In 2018 I decided to start my remote freelance career and to continue to expand my Full Stack Developer knowledge by finishing the last FreeCodeamp projects f…

WPF查找子控件和父控件方法

原文:WPF查找子控件和父控件方法public List<T> GetChildObjects<T>(DependencyObject obj, string name) where T : FrameworkElement{DependencyObject child null;List<T> childList new List<T>();for (int i 0; i < VisualTreeHelper.GetCh…

zabbix简介及基本安装(一)

zabbix简单介绍&#xff1a; 官网&#xff1a;可以进官网查看一下&#xff1a;https://www.zabbix.com/cn/ //英语能力有限的读者可以将由上角的语言调成汉语方便查看 功能&#xff1a;网络监控、服务器监控、云监控、服务监控等。 介绍&#xff1a;zabbix&#xff08;[…

聚类基本概念及常见聚类算法和EM算法

1. 基本概念 1.1 定义 聚类&#xff1a;发现数据中分组聚集的结构&#xff0c;根据数据中样本与样本之间的距离或相似度&#xff0c;依据类内样本距离小&#xff08;相似度大&#xff09;、类间样本距离大&#xff08;相似度小&#xff09;将样本划分为若干组/类/簇。 基于划分…

篇三:XPath--解析Html

篇三&#xff1a;XPath--解析Html

Redis和Memcache的区别是什么

Redis和Memcache都是内存数据库&#xff0c;但它们之间还是有区别的&#xff0c;跟着ytkah看看Redis和Memcache的区别吧 Redis 支持多种数据结构&#xff0c;如string,list,dict,set,zset,hyperloglog 单线程请求&#xff0c;所有命令串行执行&#xff0c;并发情况下不需要考虑…