【问题标题】:How to capture float values from user input to get sum of float values如何从用户输入中捕获浮点值以获取浮点值的总和
【发布时间】:2019-10-02 20:54:03
【问题描述】:

作业是:

  1. 编写代码,在列表中搜索客户订购的加载项名称。
  2. 编写打印加载项名称和价格或错误消息的代码,然后编写打印总订单成本的代码。
  3. 使用以下数据执行程序,验证输出是否正确:

    Cream
    Caramel
    Whiskey
    chocolate
    Chocolate
    Cinnamon
    Vanilla
    

我已经执行了分配的主要部分,但不明白如何捕获先前输入的值以求和/相加。

我的代码:

# Declare variables.
NUM_ITEMS = 5 # Named constant

# Initialized list of add-ins
addIns = ["Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"]

# Initialized list of add-in prices
addInPrices = [.89, .25, .59, 1.50, 1.75]
 # Flag variable
orderTotal = 2.00  # All orders start with a 2.00 charge

# Get user input
# 
addIn = ""

addIn = input("Enter coffee add-in or XXX to quit: ")
# Write the rest of the program here.
while addIn != "XXX":
    foundIt = False

    for i in range(0, len(addInPrices)):
        price = addInPrices[i]
        product = addIns[i]
        if addIn == product: 
            foundIt = True
            break

    if foundIt == True:

        print("{} Price is ${}".format(product,price))

    else: 
        print("Sorry, we do not carry that.")

    addIn = input("Enter coffee add-in or XXX to quit: ")

# MY COMMENT --- Want to create new list from input above when foundIT == True and sum total to print out total order cost.
    newList=[]  #Create new list to grab values when foundIt == True
    while foundIt == True:

        addCost=price
        newList.extend(addCost)

        foundIt == True
        break

    else:
        foundIt == False

    print(newList)


print("Order Total is ${}".format(orderTotal))

我发现我一直在尝试迭代浮点数(例如addCost、price 等)或迭代不允许的“布尔”。我是否应该在代码前面的列表中捕获用户的输入,以便为练习的最后一步求和?

除了创建一个列表来解决这个练习之外,我还应该考虑其他的事情吗?如果有,请分享。

【问题讨论】:

    标签: python list floating-point sum


    【解决方案1】:

    您实际上想要append,而不是列表中的extend。 extend 将另一个列表连接到您的列表中,append 将一个值添加到列表中。

    但实际上,我们根本不需要此列表来完成您想做的事情。相反,我们可以在考虑项目时将价格添加到总数中。我们也可以在这里打印价格,只需依靠foundIt标志输出错误信息

    # Declare variables.
    NUM_ITEMS = 5 # Named constant
    
    # Initialized list of add-ins
    addIns = ["Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"]
    
    # Initialized list of add-in prices
    addInPrices = [.89, .25, .59, 1.50, 1.75]
    # Flag variable
    orderTotal = 2.00  # All orders start with a 2.00 charge
    # Get user input
    # 
    addIn = ""
    addIn = input("Enter coffee add-in or XXX to quit: ")
    # Write the rest of the program here.
    while addIn != "XXX":
    
        foundIt = False
    
        for i in range(len(addIns)):
            if addIn == addIns[i]:
                print("Found match!")
                orderTotal += addInPrices[i]
                foundIt = True
                print("{} Price is ${}".format(addIns[i],addInPrices[i]))
                addIn = input("Enter coffee add-in or XXX to quit: ")
                continue
    
        print("Sorry, we do not carry that.")
        addIn = input("Enter coffee add-in or XXX to quit: ")
    
    print("Order Total is ${}".format(orderTotal))
    

    【讨论】:

    • 不确定这是否是提出澄清问题的正确论坛,但在我的代码中,我被“指导”进行测试,因此两条 foundIt==True 行。每次写代码都需要检查以确保它第一次通过吗?也不确定orderTotal += addInPrices[i] 回溯是如何在您的线路上发生的,但它对我来说是 addCost=price 错误:浮动不可迭代。难道是我没有在[]中包含价格?感谢您的帮助。
    • 我认为您的错误实际上是在 newList.extend(addCost) 下面的行上。 addCost = price 应该始终有效,无论价格或附加成本是多少,因为它刚刚分配
    【解决方案2】:

    应该是列表,而不是可变的 addcost。扩展运算符仅适用于列表。

    # Declare variables.
    NUM_ITEMS = 5 # Named constant
    
    # Initialized list of add-ins
    addIns = ["Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"]
    
    # Initialized list of add-in prices
    addInPrices = [.89, .25, .59, 1.50, 1.75]
     # Flag variable
    orderTotal = 2.00  # All orders start with a 2.00 charge
    
    # Get user input
    # 
    addIn = ""
    
    addIn = input("Enter coffee add-in or XXX to quit: ")
    # Write the rest of the program here.
    while addIn != "XXX":
        foundIt = False
    
        for i in range(0, len(addInPrices)):
            price = addInPrices[i]
            product = addIns[i]
            if addIn == product: 
                foundIt = True
                break
    
        if foundIt == True:
    
            print("{} Price is ${}".format(product,price))
    
        else: 
            print("Sorry, we do not carry that.")
    
        addIn = input("Enter coffee add-in or XXX to quit: ")
    
    # MY COMMENT --- Want to create new list from input above when foundIT == True and sum total to print out total order cost.
        newList=[]  #Create new list to grab values when foundIt == True
        while foundIt == True:
    
            addCost=[price]
            newList.extend(addCost)
    
            foundIt == True
            break
    
        else:
            foundIt == False
    
        print(newList)
    
    
    print("Order Total is ${}".format(orderTotal+sum(addCost)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多