如何将其转换为while循环程序?

2024-06-28 16:13:20 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码是上周作业的程序。任务是创建一个程序,您可以作为出纳输入信息,它将根据输入的信息为您汇总所有信息。本周任务的一部分是增强它,以便程序可以使用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='')

Tags: or程序formatisthisitempricesep
2条回答

总是很高兴看到人们学习编程。Python是一个很好的语言,有大量的资源。话虽如此,请检查下面的代码,如果您有兴趣看到它运行,请单击下面的链接。它是一本GoogleColab笔记本,它几乎是一个用浏览器开发的python环境

  # 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

塞纳里奥一号: OrgPrice: 100

欢迎来到StackOverflow!从教授给您的示例输出来看,似乎您需要使用break条件将当前代码(不包括常量声明)包装在while True循环中,但还需要添加另一个名为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'))

这是编辑版本的输出:

Original ticket price or 0 to quit: $10
Is this item reduced?n 
Is this item taxable?y
Here's the breakdown for this item: 
Orginal Price $10.00
Reduced Price $0.00
Final Price $10.00
7% Sales Tax $0.70
Total amount due $10.70

Original ticket price or 0 to quit: $23123123123
Is this item reduced?n
Is this item taxable?y
Here's the breakdown for this item: 
Orginal Price $23,123,123,123.00
Reduced Price $0.00
Final Price $23,123,123,123.00
7% Sales Tax $1,618,618,618.61
Total amount due $24,741,741,741.61

Original ticket price or 0 to quit: $0

Total amount due for all items: $ 24,741,741,752.31

如果您想了解python中while循环的更多信息,可以查看以下链接:https://www.w3schools.com/python/python_while_loops.asp

相关问题 更多 >