test: 博客美化中……

python之初级购物车小程序

作业需求:

  

 

思路与源码:

 

复制代码
username = input("Username:")
password = input("Password:")
print(username)
print(password)

print("Welcome logging".center(50,'-'))

save_user = open("save_username.txt", "w")  #打开文件
save_user.write(username)                      #将文件用户密码保存
save_user.write(password)



salary = input("Input your salary:")   #输入salary

if salary.isdigit():              #判断salary是否数字
    salary = int(salary)
else:
    print("Invalid data type...please input again.")
    exit()

welcome_msg = "Welcome to Spping Mall".center(50,'-')
print(welcome_msg)          #输出欢迎界面

product_list = [            #商品列表,里面放元组(只读列表)
    ('Ipad',7000),
    ('Iphone',5400),
    ('Book',40),
    ('fruit',14.4),
    ('Car',150000),
    ('Bike',250)
]

shopping_car = []

exit_flag = False
while not exit_flag:
    print("Product list".center(50,'-'))
    for item in enumerate(product_list):    #输出商品列表
        index = item[0]                     #eg:item == (0, ('Ipad', 7000)) 商品序号,从0开始
        prod_name = item[1][0]              #重要
        prod_price = item[1][1]
        print(index,':',prod_name,prod_price)

    user_choice = input("[q=quit,c=check]What do you want to buy?:")
    if user_choice.isdigit():        #输入的是数字,选商品
        user_choice = int(user_choice)
        if 0 <= user_choice < len(product_list):   #输出的数字不超界
            p_item = product_list[user_choice]     #选中的元组,eg:('Ipad',7000)
            count_of_prod = int(input("How many do you want to buy?:"))   #输入购买商品数量
            if p_item[1] * count_of_prod <= salary:                #钱够
                for i in range(count_of_prod):                    #放入购物车
                    shopping_car.append(p_item)
                salary -= p_item[1] * count_of_prod                #扣钱
                print("%s in your Shopping_car;and your current balance is %s" % (p_item ,salary))
            else:
                #余额不足
                print("your balence is %s;and You have no enough money to buy it!" % salary)
        else:
            print("The number go beyond.Try again...")
    else:                  #user_choice == 'q' or 'quit':   这样是错误的!!!
            if user_choice == 'q' or user_choice == 'quit':                #输入q退出
                print('Purchased product as below'.center(50, '*'))
                print(shopping_car)
                print('Your balance is %s' % salary)
                print('End'.center(50,'*'))
                exit_flag = True
            elif user_choice == 'c' or user_choice == 'check':              #输入c检查,输出购物车列表
                print('Purchased product as below'.center(50,'*'))
                for item in shopping_car:
                    print(item)
                print('Your balance is %s' % salary)
            else:
                print("Please input the number of product...")
View Code
复制代码

 

 

 

运行结果:

 

复制代码
C:\Python34\python3.exe C:/Users/Administrator/PycharmProjects/laonanhai/day2/shopping优化.py
Username:zcl
Password:qwe
zcl
qwe
-----------------Welcome logging------------------
Input your salary:1000
--------------Welcome to Spping Mall--------------
-------------------Product list-------------------
0 : Ipad 7000
1 : Iphone 5400
2 : Book 40
3 : fruit 14.4
4 : Car 150000
5 : Bike 250
[q=quit,c=check]What do you want to buy?:4
How many do you want to buy?:1
your balence is 1000;and You have no enough money to buy it!
-------------------Product list-------------------
0 : Ipad 7000
1 : Iphone 5400
2 : Book 40
3 : fruit 14.4
4 : Car 150000
5 : Bike 250
[q=quit,c=check]What do you want to buy?:5
How many do you want to buy?:2
('Bike', 250) in your Shopping_car;and your current balance is 500
-------------------Product list-------------------
0 : Ipad 7000
1 : Iphone 5400
2 : Book 40
3 : fruit 14.4
4 : Car 150000
5 : Bike 250
[q=quit,c=check]What do you want to buy?:2
How many do you want to buy?:2
('Book', 40) in your Shopping_car;and your current balance is 420
-------------------Product list-------------------
0 : Ipad 7000
1 : Iphone 5400
2 : Book 40
3 : fruit 14.4
4 : Car 150000
5 : Bike 250
[q=quit,c=check]What do you want to buy?:c
************Purchased product as below************
('Bike', 250)
('Bike', 250)
('Book', 40)
('Book', 40)
Your balance is 420
-------------------Product list-------------------
0 : Ipad 7000
1 : Iphone 5400
2 : Book 40
3 : fruit 14.4
4 : Car 150000
5 : Bike 250
[q=quit,c=check]What do you want to buy?:q
************Purchased product as below************
[('Bike', 250), ('Bike', 250), ('Book', 40), ('Book', 40)]
Your balance is 420
***********************End************************

Process finished with exit code 0
View Code
复制代码

 

 

 

流程图:

  先分享下画在线流程图的网址https://www.processon.com/tour

 

 

posted @   前程明亮  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示