首页 > 解决方案 > 尝试用python做一个基本的网上商店代码

问题描述

我是大学的预科生,所以我对编码很陌生。我正在为此苦苦挣扎,需要一些帮助,这是我目前编写的问题和代码:

“编写一个python程序来模拟一个在线商店。
程序应该首先显示产品列表及其价格。应该至少提供4种产品。程序应该要求用户选择产品,然后询问用户输入他们需要的产品数量。然后程序应该允许用户继续选择更多的产品和数量,直到他们输入一些东西来表明他们想要结束程序(例如给定的数字或“q”或“退出”) . 然后程序应该告诉用户他们选择的产品的总金额。

        shopping_basket = {}
        print("Welcome to the online drink store!\nThese are the drinks we offer\n1. Lemonade: £1.50\n2. 
        Coke: £2.00\n3. Fanta £1.00\n4. Water: £0.50")

        Price = {"Lemonade": 1.50, "Coke": 2.00, "Fanta": 1.00, "Water": 0.50 }

        option = int(input("Which drink would you like to purchase?: "))

        while option!= 0:
            if option == 1:
            qnty = int(input("Enter the quantity: "))
            total = qnty * 1.50
            print("The price is: " + str(total))
        elif option == 2:
            qnty = int(input("Enter the quantity: "))
            total = qnty * 2.00
            print("The price is: " + str(total))
        elif option == 3:
            qnty = int(input("Enter the quantity: "))
            total = qnty * 1.00
            print("The price is: " + str(total))
        elif option == 4:
            qnty = int(input("Enter the quantity: "))
            total = qnty * 0.50
            print("The price is: " + str(total))

            print("Would you like another item? enter Yes or No:")
        else:
            print("The total price of your basket is: " , total = Price * qnty)

这是我尝试过的代码,但在说明价格之后,它只是不断地询问数量。

标签: python

解决方案


我不想发布新答案,而是发表评论,但可惜,声誉不够。

我只是想添加到Daemon Painter的答案,并说最终的总账单也不起作用,因为它是将 dict 乘以 int。

可行的是在循环外初始化 total 变量以及新的 total_cost 变量,然后放置:

total_cost += total

在 while 循环内但在 if 条件外。这样最后一行可以是:

print("The total price of your basket is: ", total_cost)

编辑:代码,按预期工作:

    price = {"Lemonade": 1.50, "Coke": 2.00, "Fanta": 1.00, "Water": 0.50}
    shopping_basket = {}

    print("Welcome to the online drink store!\nThese are the drinks we offer\n1. Lemonade: £1.50\n2. Coke: £2.00\n3. Fanta £1.00\n4. Water: £0.50")

    buy_another_flag = 1
    total_cost, total = 0, 0

    while buy_another_flag != 0:
        option = int(input("Which drink would you like to purchase?: "))

        if option == 1:
            qnty = int(input("Enter the quantity: "))
            total = qnty * 1.50
            print("The price is: " + str(total))
        elif option == 2:
            qnty = int(input("Enter the quantity: "))
            total = qnty * 2.00
            print("The price is: " + str(total))
        elif option == 3:
            qnty = int(input("Enter the quantity: "))
            total = qnty * 1.00
            print("The price is: " + str(total))
        elif option == 4:
            qnty = int(input("Enter the quantity: "))
            total = qnty * 0.50
            print("The price is: " + str(total))

        total_cost += total

        buy_another_flag = int(input("Would you like another item? enter Yes (1) or No (0):"))
    print("The total price of your basket is: ", total_cost)

推荐阅读