以下代码是上周分配作业的程序。任务是创建一个程序,您可以将其作为收银员输入信息,并根据输入的信息为您汇总所有信息。本周任务的一部分是增强功能,以便程序可以使用while循环处理多个项目。改进后的程序应该输出所有项目的总计。
这是教授正在寻找的输出示例。
Original ticket price: $100
Is this item reduced?y
Is this item taxable?y
Here is your bill
Orginal Price $100.00
Reduced Price $25.00
Final Price $75.00
7% Sales Tax $5.25
Total amount due $80.25
Original ticket price: $75
Is this item reduced?y
Is this item taxable?n
Here is your bill
Orginal Price $75.00
Reduced Price $18.75
Final Price $56.25
7% Sales Tax $0.00
Total amount due $56.25
Total amount due is $136.50
# Enter constants for sale & salesTax
SALE = .25
SALES_TAX = .07
# Enter the ticket price of the item
origPrice = float(input('Original ticket price or 0 to quit: $'))
# Is the item reduced? Y/y or N/n - use if/elif to determine salePrice
reduced = input('Is this item reduced?')
if reduced == 'Y' or reduced == 'y':
salePrice = origPrice * SALE
elif reduced == 'N' or reduced == 'n':
salePrice = 0.00
# Enter constant for finalPrice = origPrice - salePrice
finalPrice = origPrice - salePrice
# Is the item taxable? Y/y or N/n - use if/elif to determine tax
taxable = input('Is this item taxable?')
if taxable == 'Y' or taxable == 'y':
tax = finalPrice * SALES_TAX
elif taxable == 'N' or taxable == 'n':
tax = 0.00
# Enter all Print Statements
print('Here is your bill')
print('Orginal Price $', format(origPrice, ',.2f'),sep='')
print('Reduced Price $', format(salePrice, ',.2f'),sep='')
print('Final Price $', format(finalPrice, ',.2f'),sep='')
print('7% Sales Tax $', format(tax, ',.2f'),sep='')
print('Total amount due $', format(finalPrice + tax, ',.2f'),sep='')
答案 0 :(得分:1)
总是很高兴看到人们学习编程。 Python是一个很棒的语言,那里有很多资源。话虽如此,请查看下面的代码,如果您有兴趣运行它,请单击下面的链接。这是一本Google Colab笔记本,它是从您的浏览器开发的python env。
# Enter constants for sale & salesTax
SALE = .25
SALES_TAX = .07
Order = []
while True:
# Enter the ticket price of the item
tmp = float(input('Original ticket price or 0 to quit: $'))
if 0 == tmp:
break
else:
origPrice = tmp
# Is the item reduced? Y/y or N/n - use if/elif to determine salePrice
reduced = input('Is this item reduced?')
if reduced == 'Y' or reduced == 'y':
salePrice = origPrice * SALE
elif reduced == 'N' or reduced == 'n':
salePrice = 0.00
# Enter constant for finalPrice = origPrice - salePrice
finalPrice = origPrice - salePrice
# Is the item taxable? Y/y or N/n - use if/elif to determine tax
taxable = input('Is this item taxable?')
if taxable == 'Y' or taxable == 'y':
tax = finalPrice * SALES_TAX
elif taxable == 'N' or taxable == 'n':
tax = 0.00
result = (finalPrice + tax)
Order.append(['Orginal Price ${0} '.format(origPrice, '.2f'), 'Reduced Price ${0} '.format(salePrice, '.2f'), 'Final Price ${0} '.format(finalPrice, '.2f'),
'7% Sales Tax ${0} '.format(tax, '.2f'), 'Total amount due ${0} '.format(result, '.2f')])
# Enter all Print Statements
print('\n')
for i in Order:
print(i[0])
print(i[1])
print(i[2])
print(i[3])
print(i[4])
链接Colab:https://colab.research.google.com/drive/1S64fGVM1rQTv05rJBlvjOVrwHQFm8faK?usp=sharing
答案 1 :(得分:0)
欢迎使用StackOverflow!从教授给您的示例输出中,您似乎需要使用中断条件将当前代码(不包括常量声明)包装在while True
循环中,还需要添加另一个名为{{1}的变量}。每次添加新项目时,此变量都会更改。如果要应用更改,则应如下所示:
totalAmountDue
这是编辑版本的输出:
# Enter constants for sale & salesTax
SALE = .25
SALES_TAX = .07
# Counter for the total amount of money from all items, this includes tax as well
totalAmountDue = 0
while True:
# Enter the ticket price of the item
origPrice = float(input('Original ticket price or 0 to quit: $'))
# Breaks out of the loop once the user wants to quit
if (origPrice == 0):
break
# Is the item reduced? Y/y or N/n - use if/elif to determine salePrice
reduced = input('Is this item reduced?')
if reduced == 'Y' or reduced == 'y':
salePrice = origPrice * SALE
elif reduced == 'N' or reduced == 'n':
salePrice = 0.00
# Enter constant for finalPrice = origPrice - salePrice
finalPrice = origPrice - salePrice
# Is the item taxable? Y/y or N/n - use if/elif to determine tax
taxable = input('Is this item taxable?')
if taxable == 'Y' or taxable == 'y':
tax = finalPrice * SALES_TAX
elif taxable == 'N' or taxable == 'n':
tax = 0.00
# Adds the final price of this product to the total price of all items
totalAmountDue += finalPrice + tax
# Enter all Print Statements
print("Here's the breakdown for this item: ")
print('Orginal Price $', format(origPrice, ',.2f'),sep='')
print('Reduced Price $', format(salePrice, ',.2f'),sep='')
print('Final Price $', format(finalPrice, ',.2f'),sep='')
print('7% Sales Tax $', format(tax, ',.2f'),sep='')
print('Total amount due $', format(finalPrice + tax, ',.2f'), "\n",sep='')
print("\nTotal amount due for all items: $", format(totalAmountDue, ',.2f'))
如果您想了解有关python中while循环的更多信息,可以查看以下链接:https://www.w3schools.com/python/python_while_loops.asp