简体   繁体   English

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

[英]Python List/Dictionary not retaining the user input

#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()

My above program is not able to store the user input for add/update new programs.我的上述程序无法存储用于添加/更新新程序的用户输入。 It shows at the runtime the new added items but when I run the program again it shows the default values.它在运行时显示新添加的项目,但当我再次运行程序时它显示默认值。 Also I want to change the part where I am doing calculations of products ( Under the #Membership comment).此外,我想更改我正在计算产品的部分(在#Membership 评论下)。 As I have only used the default values there But in the case the user adds new items I want to be able to calculate that part also.因为我只在那里使用了默认值但是在用户添加新项目的情况下我也希望能够计算那部分。 I am having trouble in figuring out that part.我在弄清楚那部分时遇到了麻烦。

I am a beginner at programming, so any help or suggestions much appreciated.我是编程的初学者,所以非常感谢任何帮助或建议。

Also, for this Assignment our instructor has restricted us to use any modules.此外,对于此作业,我们的讲师限制我们使用任何模块。

you have something wrong in this code,but don`t worry about that你这段代码有问题,但别担心

first of all,首先,

 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))

all the items["shirt"], items[Tshirt], items[Trouser]所有项目[“衬衫”],项目[T恤],项目[裤子]

you can use items[Choose_Product] in your program like this你可以像这样在你的程序中使用 items[Choose_Product]

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))

when you want add/update new items you don`t need add anything当你想添加/更新新项目时,你不需要添加任何东西

secondly,I think you should add menu information in the while loop其次,我认为你应该在 while 循环中添加菜单信息

when user finish a operate,he/she can do others当用户完成一项操作后,他/她可以做其他操作

finally,when people,input price,we should change str to int or float最后,当人们输入价格时,我们应该将 str 更改为 int 或 float

if you don`t do this如果你不这样做

TypeError: can't multiply sequence by non-int of type 'xxx'类型错误:无法将序列乘以类型为“xxx”的非整数

sorry,English is not my native language,and I hope this answer can help you抱歉,英语不是我的母语,希望这个回答能帮到你

I write a new file based on your code, maybe it can help you understand in few minutescode gist我根据你的代码写了一个新文件,也许它可以帮助你在几分钟内理解代码要点

BTW,don`t copy code to your instructor.顺便说一句,不要将代码复制给您的讲师。 lol.哈哈。 you need understand it你需要了解它

have a good day.祝你有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM