1

私のプログラムの目的は、仮想のマクドナルドのレジ係になることです。プログラムの最初の部分では、レジ係が顧客の注文を 1 つ取ります。while ループのおかげで、顧客は好きなだけ商品を注文できます。最後に、税金、小計、および最終請求書が表示されます。ユーザーが支払う金額とその釣り銭。ここで、プログラムで最初の部分をすべて実行する必要がありますが、並んでいる顧客と同じ数の顧客に対してこれを実行するには、ユーザー/レジ係が必要です。2 つの while ループを入れ子にしてみましたが、何か間違ったことをしていて、何が原因かわかりません。誰かが私を助けることができますか?ちなみに、最初の部分は100%動作します。

最初の部分

num1 = 4.87
num2 = 5.03
num3 = 5.50
num4 = 9.45
num5 = 1.29
num6 = 2.19
num7 = 2.29
itemnum = 0
Subtotal = 0
tax = 0.0565
amtgiven = 0
change = 0
quantity = 0
foodprice = 0
Totalprice1 = 0
Totalprice2 = 0
Totalprice3 = 0
Totalprice4 = 0
Totalprice5 = 0
Totalprice6 = 0
Totalprice7 = 0
billtax = 0
finalbill = 0
change = 0

print "Welcome to Virtual McDonald's!"   "Would you like to order a food item"
print "Item:                     Meal/tem:                          Price:"
print "1                          Big Mac Meal                        4.87"
print "2                          Quarter Pounder Meal                5.03"
print "3                          Chicken Nuggets Meal (5 piece)      5.50"
print "4                          ChickenNuggets Meal (10 piece)      9.45"
print "5                          Apple Pie                           1.29"
print "6                          Large Drink                         2.19"
print "7                          Large Fries                         2.29"

itemnum = input("Enter the item you would like to purchase! ")
quantity = input("How many of this item do you want?")
while itemnum != -1:

    if itemnum == 1:
        foodprice = quantity * num1
        Totalprice1 = foodprice

    if itemnum == 2:
        foodprice = quantity * num2
        Totalprice2 = foodprice

    if itemnum == 3:
        foodprice = quantity * num3
        Totalprice3 = foodprice

    if itemnum == 4:
        foodprice == quantity * num4
        Totalprice4 = foodprice

    if itemnum == 5:
        foodprice = quantity * num5
        Totalprice5 = foodprice

    if itemnum == 6:
        foodprice == quantity * num6
        Totalprice6 = foodprice

    if itemnum == 7:
        foodprice = quantity * num7
        Totalprice7 = foodprice   

    subtotal = Totalprice1 + Totalprice2 + Totalprice3 + Totalprice4 + Totalprice5 + Totalprice6 + Totalprice7 
    billtax = subtotal * tax
    finalbill = subtotal + billtax
    itemnum = input("Enter the item you would like to purchase! ")
    quantity = input("How many of this item do you want?")

print "Your total bill without tax is... ", round(subtotal,2)
print "Your total tax is... ", round(billtax,2)
print "Your final bill is... ", round(finalbill,2)
amtgiven = input ("How much do you want to pay with?")
change = amtgiven - finalbill
print "Your change is... ", round(change,2)

第二部

num1 = 4.87
num2 = 5.03
num3 = 5.50
num4 = 9.45
num5 = 1.29
num6 = 2.19
num7 = 2.29
itemnum = 0
Subtotal = 0
tax = 0.0565
amtgiven = 0
change = 0
quantity = 0
foodprice = 0
Totalprice1 = 0
Totalprice2 = 0
Totalprice3 = 0
Totalprice4 = 0
Totalprice5 = 0
Totalprice6 = 0
Totalprice7 = 0
billtax = 0
finalbill = 0
change = 0
customer = 0

print "Welcome to Virtual McDonald's!"   "Would you like to order a food item"
print "Item:                     Meal/tem:                          Price:"
print "1                          Big Mac Meal                        4.87"
print "2                          Quarter Pounder Meal                5.03"
print "3                          Chicken Nuggets Meal (5 piece)      5.50"
print "4                          ChickenNuggets Meal (10 piece)      9.45"
print "5                          Apple Pie                           1.29"
print "6                          Large Drink                         2.19"
print "7                          Large Fries                         2.29"


customer = raw_input ("Would you like to order? (If not type No)")
while customer != "No":

    while itemnum != -1: 
        itemnum = input("Enter the item you would like to purchase! ")
        quantity = input("How many of this item do you want? ")

        if itemnum == 1:
            foodprice = quantity * num1
            Totalprice1 = Totalprice1 + foodprice

        if itemnum == 2:
            foodprice = quantity * num2
            Totalprice2 = Totalprice2 + foodprice

        if itemnum == 3:
            foodprice = quantity * num3
            Totalprice3 = Totalprice3 + foodprice

        if itemnum == 4:
            foodprice = quantity * num4
            Totalprice4 = Totalprice4 + foodprice

        if itemnum == 5:
            foodprice = quantity * num5
            Totalprice5 = Totalprice5 + foodprice

        if itemnum == 6:
            foodprice = quantity * num6
            Totalprice6 = Totalprice6 + foodprice

        if itemnum == 7:
            foodprice = quantity * num7
            Totalprice7 = Totalprice7 + foodprice

    itemnum = input("Enter the item you would like to purchase! ")
    quantity = input("How many of this item do you want? ")
    subtotal = Totalprice1 + Totalprice2 + Totalprice3 + Totalprice4 + Totalprice5 + Totalprice6 + Totalprice7 
    billtax = subtotal * tax
    finalbill = subtotal + billtax        
    print "Your total bill without tax is... ", round(subtotal,2)
    print "Your total tax is... ", round(billtax,2)
    print "Your final bill is... ", round(finalbill,2)
    amtgiven = input ("How much do you want to pay with? ")
    change = amtgiven - finalbill
    print "Your change is... ", round(change,2)
    customer = raw_input ("Would you like to order? (If not type No)")

#出力

2 番目のプログラムを実行すると、次のように出力されます。

Welcome to Virtual McDonald's!Would you like to order a food item
Item:                     Meal/tem:                          Price:
1                          Big Mac Meal                        4.87
2                          Quarter Pounder Meal                5.03
3                          Chicken Nuggets Meal (5 piece)      5.50
4                          ChickenNuggets Meal (10 piece)      9.45
5                          Apple Pie                           1.29
6                          Large Drink                         2.19
7                          Large Fries                         2.29
Would you like to order? (If not type No) yes
Enter the item you would like to purchase! 1
How many of this item do you want? 2
Enter the item you would like to purchase! 1
How many of this item do you want? 4
Your total bill without tax is...  9.74
Your total tax is...  0.55
Your final bill is...  10.29
How much do you want to pay with? 11
Your change is...  0.71
Enter the item you would like to purchase!

(いくつかのメニュー項目を入力した後、プログラムはループの実行を停止し、請求書に直行します。また、請求書の後、別の顧客であるかどうかをユーザーに尋ねません。)

最初の部分を実行すると、次のように出力されます。

Welcome to Virtual McDonald's!Would you like to order a food item
Item:                     Meal/tem:                          Price:
1                          Big Mac Meal                        4.87
2                          Quarter Pounder Meal                5.03
3                          Chicken Nuggets Meal (5 piece)      5.50
4                          ChickenNuggets Meal (10 piece)      9.45
5                          Apple Pie                           1.29
6                          Large Drink                         2.19
7                          Large Fries                         2.29
Enter the item you would like to purchase! 1
How many of this item do you want?4
Enter the item you would like to purchase! 2
How many of this item do you want?1
Enter the item you would like to purchase! -1
How many of this item do you want?-1
Your total bill without tax is...  24.51
Your total tax is...  1.38
Your final bill is...  25.89
How much do you want to pay with? 26
Your change is...  0.11

最初の部分が出力したものを2番目の部分で出力したい。顧客に変更を加えた後を除いて、他のループを実行したいので、別の顧客がいるかどうかをユーザーに尋ねます。ユーザーが「はい」と入力すると、プログラムは別の顧客のために最初からやり直します。顧客がいなくなった場合にのみ、ユーザーは「いいえ」を入力してプログラムを終了します。(Pythonシェルに行って、これらの出力結果をコピーしました。右側の数字は私が入力した数字です。)

4

1 に答える 1