首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python列表/字典不保留用户输入

Python列表/字典不保留用户输入
EN

Stack Overflow用户
提问于 2022-04-03 03:10:05
回答 1查看 70关注 0票数 -2
代码语言:javascript
运行
复制
#PART 1

Customers=[]
Customer_name=input("Please enter your name: ") #User name Input
Customers.append(Customer_name)
Mem_Customers=[]
items={"Shirt":100, "Trouser":150, "Shorts":50, "Tshirt":100}  
Products=["Shirt","Trouser","Shorts","Tshirt"] 
Price=[100,150,50,100]

def menu():
print(" Welcome to RMIT Retail Management System")
print()
print("#"*40)
print()
print("Please choose form the following options")
print("1: Place an order")
print("2: Add/Update Products and Prices")
print("3: Display existing Customers")
print("4: Display existing Customers with Membership")  
print("5: Display existing Products")  
print("0: Exit the menu" ) 
print()  
print("#"*40)
menu()
option=input(" Choose one Option:")

while int(option)!=0:
if int(option)==1:
Choose_Product=input("Please enter the product you want to choose (valid product only i.e Shirt,Trouser,Shorts,Tshirt) : ")

        while not Choose_Product in items:
            print("Please enter a valid product")
            Choose_Product=input("Please enter the product you want to choose (valid product only i.e Shirt,Trouser,Shorts,Tshirt) : ")
        
        
        valid_quantity= False
        while not valid_quantity:
            Quantity=input("Please enter the quantity of the products required(Positive integer only): ") 
            if Quantity.strip().isdigit() and int(Quantity)>0:
                valid_quantity= True
            else:
                print("Please enter a valid integer")
    
        
        
        response=False
        while not response:  
            Membership=input("Please specify that you want a membership or not Y/N : ") 
            if Membership  in "Y" "N":
                response=True
            else:
                print("Please enter a valid response")
    
        
    
    
    
    # Membership
        if Membership=="Y" and Choose_Product=="Shirt":
            Mem_Customers.append(Customer_name)
            print(Customer_name  +"purchases"+str(Quantity)+ "x"+ Choose_Product)
            print("Unit price :      100 AUD")
            print(Customer_name+"Gets a discount of 5%")
            totalprice=(items["Shirt"]*int(Quantity))
            finalprice=totalprice-(totalprice*0.05)
            print("Total price : "+str(finalprice))
    
        if Membership=="Y" and Choose_Product=="Trouser":
            Mem_Customers.append(Customer_name)
            print(Customer_name +"purchases"+str(Quantity)+ "x"+ Choose_Product)
            print("Unit price :      150 AUD")
            print(Customer_name+"Gets a discount of 5%")
            totalprice=(items["Trouser"]*int(Quantity))
            finalprice=totalprice-(totalprice*0.05)
            print("Total price : "+str(finalprice))
    
        if Membership=="Y" and Choose_Product=="Shorts":
                Mem_Customers.append(Customer_name)
                print(Customer_name +"purchases"+str(Quantity)+ "x"+ Choose_Product)
                print("Unit price :      50 AUD")
                print(Customer_name+"Gets a discount of 5%")
                totalprice=(items["Shorts"]*int(Quantity))
                finalprice=totalprice-(totalprice*0.05)
                print("Total price : "+str(finalprice))
    
        if Membership=="Y" and Choose_Product=="Tshirt":
                Mem_Customers.append(Customer_name)
                print(Customer_name +"purchases"+str(Quantity)+ "x"+ Choose_Product)
                print("Unit price :      100 AUD")
                print(Customer_name+"Gets a discount of 5%")
                totalprice=(items["Tshirt"]*int(Quantity))
                finalprice=totalprice-(totalprice*0.05)
                print("Total price : "+str(finalprice))
    
        # No Membership
    
        if Membership=="N" and Choose_Product=="Shirt":             
                print(Customer_name +"purchases"+str(Quantity)+ "x"+ Choose_Product)
                print("Unit price :      100 AUD")
                totalprice=(items["Shirt"]*int(Quantity))
                finalprice=totalprice
                print("Total price : "+str(finalprice))
    
        if Membership=="N" and Choose_Product=="Trouser":
                print(Customer_name +"purchases"+str(Quantity)+ "x"+ Choose_Product)
                print("Unit price :      150 AUD")
                totalprice=(items["Trouser"]*int(Quantity))
                finalprice=totalprice
                print("Total price : "+str(finalprice))
    
        if Membership=="N" and Choose_Product=="Shorts":
                print(Customer_name +"purchases"+str(Quantity)+ "x"+ Choose_Product)
                print("Unit price :      50 AUD")
        
                totalprice=(items["Shorts"]*int(Quantity))
                finalprice=totalprice
                print("Total price : "+str(finalprice))
    
        if Membership=="N" and Choose_Product=="Tshirt":
                print(Customer_name +"purchases"+str(Quantity)+ "x"+ Choose_Product)
                print("Unit price :      100 AUD")
                totalprice=(items["Tshirt"]*int(Quantity))
                finalprice=totalprice
                print("Total price : "+str(finalprice))  
               
    
    
    
    if int(option)==2: 
        add_product=input("Please enter the new products separated by , :").split(",")
        Products.append(add_product)
        add_price=input("Please enter the price of the products separated by , :").split(",")
        Price.append(add_price)
        for i in range(len(add_product)):
            add_product[i]=add_product[i]
            add_price[i]=add_price[i]
    
            if add_product[i] in items:
                print("The product already exists and prices will be updated")
                items[add_product[i]]=add_price[i]
            else:
                print("New product has been entered and will be updated ")
                items[add_product[i]]=add_price[i]
            
    if int(option)==3:
        print()
        print("The existing customers are:""\n")
        print(list(set(Customers)))
        break
    if int(option)==4:
        print()
        print("The customers with membership are:""\n")
        print(list(set(Mem_Customers)))
        break
    if int(option)==5:
        print()
        print("The existing products are:""\n")
        print(list(set(Products)))
        break

if int(option)==0:
print()
print("Thanks for using our program bye..""\n")

menu()

我的上述程序无法存储用户输入的添加/更新新程序。它在运行时显示新添加的项,但当我再次运行程序时,它将显示默认值。此外,我想改变的部分,我正在做的产品计算(在#成员意见)。因为我只在那里使用了默认值,但是在这种情况下,用户添加了新的项,我也希望能够计算这个部分。我很难弄清楚那部分。

我是一个编程初学者,所以任何帮助或建议都非常感谢。

另外,对于这个作业,我们的老师限制我们使用任何模块。

EN

回答 1

Stack Overflow用户

发布于 2022-04-03 08:30:28

这段代码有问题,但不用担心。

首先,

如果会员== "Y“和Choose_Product ==”嫁妆“:Mem_Customers.append(Customer_name)打印(Customer_name+”购买“+str(数量)+ "x”+ Choose_Product)打印(“单价: 150澳元”)打印(Customer_name+“得到5%的折扣)总价=(商品”)*int(数量)终价=总价-(总价*0.05)打印(“总价:”+str(最终价格))

所有项目“衬衫”,itemsTshirt,itemsTrouser

您可以在您的程序中像这样使用itemsChoose_Product

代码语言:javascript
运行
复制
if Membership == "Y":

​        Mem_Customers.append(Customer_name)

​        print()

​        print(Customer_name+":"+"purchases :"+str(Quantity) + " x " + Choose_Product)

​        print("Unit price :   "+str(items[Choose_Product])+" AUD")

​        print(Customer_name+"Gets a discount of 5%")

​        totalprice = (items[Choose_Product]*int(Quantity))

​        finalprice = totalprice-((totalprice)*0.05)

​        print("Total price : "+str(finalprice))

当您想要添加/更新新项目时,您不需要添加任何内容。

其次,我认为您应该在while循环中添加菜单信息。

当用户完成操作时,他/她可以完成其他操作

最后,当人们,投入价格,我们应该改变为整数或浮动。

如果你不这么做

TypeError:不能将序列乘以'xxx‘型的非整数

对不起,英语不是我的母语,我希望这个答案能帮助你。

我根据您的代码编写了一个新文件,也许它可以在几分钟内帮助您理解代码要旨

顺便说一句,不要把代码拷贝给你的老师。LOL。你需要理解它

祝你今天愉快。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71722703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档