亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從用戶輸入中捕獲浮點值以獲取浮點值的總和

如何從用戶輸入中捕獲浮點值以獲取浮點值的總和

小唯快跑啊 2022-06-14 10:54:28
任務是:編寫代碼,在列表中搜索客戶訂購的加載項名稱。編寫打印加載項名稱和價格或錯誤消息的代碼,然后編寫打印總訂單成本的代碼。使用以下數據執行程序并驗證輸出是否正確:CreamCaramelWhiskeychocolateChocolateCinnamonVanilla我已經執行了分配的主要部分,但不明白如何捕獲先前輸入的值以求和/相加。我的代碼:# Declare variables.NUM_ITEMS = 5 # Named constant# Initialized list of add-insaddIns = ["Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"]# Initialized list of add-in pricesaddInPrices = [.89, .25, .59, 1.50, 1.75] # Flag variableorderTotal = 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等)或迭代不允許的“布爾”。我是否應該在代碼前面的列表中捕獲用戶的輸入,以便為練習的最后一步求和?除了創建一個列表來解決練習之外,我還應該考慮其他事情嗎?如果有,請分享。
查看完整描述

2 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

而不是extend在您的清單上,您實際上想要append該項目。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))


查看完整回答
反對 回復 2022-06-14
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

而不是 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)))


查看完整回答
反對 回復 2022-06-14
  • 2 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號