#購物車#1. 商品信息- 數量、單價、名稱#2. 用戶信息- 帳號、密碼、余額#3. 用戶可充值#4. 購物歷史信息#5. 允許用戶多次購買,每次可購買多件#6. 余額不足時進行提醒#7. 用戶退出時 ,輸出當次購物信息
importsys
flag_break=False
old_user=TruewhileTrue:
user_name= input('用戶名:').strip()
python基礎題?with open('old_user',encoding='utf-8') as f_old:for line inf_old:if line.strip() ==user_name:
old_user= True #判斷是老用戶標志位
password =input('密碼:').strip()
with open('shopping_user',encoding='utf-8') as f_user:for line inf_user:
username,passwd,balance=line.strip().split()if user_name == username and password ==passwd:print('登錄成功,您的余額:%s'%balance)
salary=int(balance)
python大作業。flag_break=Trueif notflag_break:
sys.exit('登錄失敗')if old_user: #執行對于老用戶打印歷史購物信息的工作
with open('shopping_history',encoding='utf-8') as f_history:
file_history=f_history.read()print('您以前購買過以下商品'.center(50,'-'))print('id 商品 數量 單價 總價')print(file_history.strip())print('end'.center(60,'-'))
shopping_cart= {} #購物車
product_list =[
python編程入門。['自行車',999],
['充電寶',555],
['茅臺酒',650],
['鉑金筆',500],
['電飯鍋',200]
]whileTrue:
Python作業。index=0for product inproduct_list:print(index,product)
index+=1choice= input('請輸入商品編號,或者q直接退出')if choice=='q':print('您已購買以下商品'.center(50,'-'))
id_count= 1total_cost=0print('id 商品 數量 單價 總價')for key inshopping_cart:
msg= ("%s\t\t%s\t\t%s\t\t%s\t\t%s"
%(id_count,
key,
python爬蟲教程?shopping_cart[key][1],
shopping_cart[key][0],
shopping_cart[key][1]*shopping_cart[key][0]
)
)print(msg)
id_count+=1total_cost+=shopping_cart[key][1]*shopping_cart[key][0]
python語言程序設計、with open('shopping_history', 'a+', encoding='utf-8') as f_history:
f_history.write('\n' +msg)print('您的總花費為:',total_cost)print('您的余額為:'+str(salary))print('end'.center(60,'-'))
with open('old_user', 'a+',encoding='utf-8') as f_old:
f_old.write('\n' +user_name)
sys.exit()
number= input('請輸入購買數量:')if choice.isdigit() andnumber.isdigit():
python有什么用,choice=int(choice)
number=int(number)if choice >= 0 and len(product_list) >=choice:
product=product_list[choice]if product[1]*number <=salary:if product[0] inshopping_cart:
shopping_cart[product[0]][1] += number #[price ,數量] 只需要把數量+1 ,加入購物車
else:
shopping_cart[product[0]]= [product[1],number]
python和java,salary-=product[1]*numberprint('您已購買 %s個%s ,您的余額為:%s'%(number,product[0],salary))else:print("商品的價格為:%s,購買數量%s個,還差%s元"%(product[1],number,product[1]*number-salary))
more_money= input('你想要充值嗎?y/n:').strip()if more_money == 'n':
sys.exit()if more_money == 'y':
recharge= int(input('充值:').strip())
salary+=rechargeelse:print('商品編號不存在,請重新輸入')else:print('編號不存在,請重新輸入')
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态