我必須撰寫的這段代碼應該顯示小計、專案數量、銷售稅和總成本
當我運行它時,程式計算輸入錯誤的價格,并且只提示我兩次價格,當它應該提示我直到我告訴它停止時。
該程式使用 while 回圈和哨兵來處理在線購買的專案總成本。計算中還有6.25%的稅率。
SENTINEL = 0
TAXRATE = 0.0625
numItems = 0
subTotal = 0
print("""/
This program will prompt you for the items you purchased online
until you enter the sentinel value of 0 to stop the process. The
program will create a running total of the items.\n\n\n""")
itemCost = float(input("Enter the price of the item(0 to stop):"))
while itemCost != SENTINEL:
if itemCost < 0:
print(f'this price is illegal!')
else:
numItems =1
subTotal =itemCost
itemCost = float(input("Enter the price of the item(0 to stop):"))
if numItems > 0:
salesTax = subTotal * TAXRATE
totalCost = subTotal salesTax
print(f'The number of items purchased is {numItems}')
print(f'The sub total is {subTotal:.2f}')
print(f'The sales tax is {salesTax:.2f}')
print(f'The total cost is {totalCost:.2f}')
else:
print(f'You need to purchase items!')
我希望這個程式會提示 infinitley,直到您輸入哨兵號碼,但它只提示兩次。它還認為 numItems 總是等于 1,而實際上它不應該。它也計算錯誤。
uj5u.com熱心網友回復:
Python 中的縮進是有意義的。您的代碼if縮進了最后一個陳述句,但您希望它位于回圈之外(您可能\n在開始文本中也缺少一個,您有/):
SENTINEL = 0
TAXRATE = 0.0625
numItems = 0
subTotal = 0
print("""\n
This program will prompt you for the items you purchased online
until you enter the sentinel value of 0 to stop the process. The
program will create a running total of the items.\n\n\n""")
itemCost = float(input("Enter the price of the item(0 to stop):"))
while itemCost != SENTINEL:
if itemCost < 0:
print(f'this price is illegal!')
else:
numItems =1
subTotal =itemCost
itemCost = float(input("Enter the price of the item(0 to stop):"))
if numItems > 0:
salesTax = subTotal * TAXRATE
totalCost = subTotal salesTax
print(f'The number of items purchased is {numItems}')
print(f'The sub total is {subTotal:.2f}')
print(f'The sales tax is {salesTax:.2f}')
print(f'The total cost is {totalCost:.2f}')
else:
print(f'You need to purchase items!')
使用if外部回圈,輸出可能如下所示:
This program will prompt you for the items you purchased online
until you enter the sentinel value of 0 to stop the process. The
program will create a running total of the items.
Enter the price of the item(0 to stop):1
Enter the price of the item(0 to stop):2
Enter the price of the item(0 to stop):3
Enter the price of the item(0 to stop):4.25
Enter the price of the item(0 to stop):0
The number of items purchased is 4
The sub total is 10.25
The sales tax is 0.64
The total cost is 10.89
請注意,您可能還應該:
- 在蛇形情況下使用變數名,即
num_items代替numItems. - 僅對全域使用變數(很少需要)使用全大寫,這
TAXRATE可能是一個很好的例子,但SENTINEL不是,因為它特定于某些回圈。 - 處理不是數字的輸入,因為它當前會使您的程式崩潰。
- 處理負面輸入(或至少考慮負面輸入可能不是商品而是折扣或退款)。
- 將提示字串放在一個字串變數中,這樣就不用重復了;或在回圈開始時僅提示一次后使用
while True:andbreak在哨兵值上 - 但我明白您可能會受到特定分配限制的限制。
uj5u.com熱心網友回復:
我編輯了一些行:
1 ) 我在 while 回圈中使用 itemCost 變數。因為回圈會想要輸入一次。
2)我改變了你的while條件,我添加了else條件。當 itemCost == 0 時,回圈將中斷。
TAXRATE = 0.0625
numItems = 0
subTotal = 0
while True:
itemCost = float(input("Enter the price of the item(0 to stop):"))
if itemCost < 0:
print(f'this price is illegal!')
elif itemCost > 0:
numItems = 1
subTotal = itemCost
else:
print("program finished.")
break
if numItems > 0:
salesTax = subTotal * TAXRATE
totalCost = subTotal salesTax
print(f'The number of items purchased is {numItems}')
print(f'The sub total is {subTotal:.2f}')
print(f'The sales tax is {salesTax:.2f}')
print(f'The total cost is {totalCost:.2f}')
else:
print(f'You need to purchase items!')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/525163.html
