了解字符串和浮点数的工作原理

问题描述 投票:0回答:1
  • 输入

    • 获取玩具名称
    • 获取单价
    • 获取订购数量
  • 流程

    • 含税总价是价格乘以数量
    • 含税总额为总价乘以7%
  • 输出

    • 打印玩具名称,以及含税总价。
    #Input (Receive info)
    charge_for_toy = float(input('\nEnter charge for toy: $'))

    #Process (Calculate info)
    SALES_TAX = .07
    sales_tax = (charge_for_toy + SALES_TAX)
    grand_total = charge_for_toy + sales_tax

    #Output (display info)
    print('\nCharge for toy = $', format(charge_for_toy, ',.2f'), sep='')
    print('Sales tax = $', format(sales_tax, ',.2f'), sep='')
    print('Grand total = $', format(grand_total, ',.2f'), sep='', end='\n\n')`

我尝试在“获取玩具名称和订购数量”中输入与我相同的行

    charge_for_toy = float(inpuit('\nEnter charge for toy: $')) 

我被告知绳子不能漂浮。我有点不知道如何继续。

python pycharm python-idle
1个回答
0
投票

我尝试在“获取玩具名称和订购数量”中输入与我相同的行

因此,如果您想要名称中包含字母,则不能使用“同一行”。

input
默认为字符串,因此您需要的是:

#Input (Receive info)
name_for_toy = input('Enter toy name: ')
charge_for_toy = float(input('Enter charge for toy: $'))
toy_quantity = int(input('Enter toy quantity: '))   # int because can't have fraction of toy

#Process (Calculate info)
SALES_TAX = 0.07
subtotal = charge_for_toy * toy_quantity
sales_tax = subtotal * SALES_TAX                    # corrected, "+ SALES_TAX" incorrect here
grand_total = subtotal + sales_tax

#Output (display info)
print('Toy name =', name_for_toy)
print('Charge for toy = $', format(charge_for_toy, ',.2f'))
print('Sales tax = $', format(sales_tax, ',.2f'))
print('Grand total = $', format(grand_total, ',.2f'), end='\n\n')
© www.soinside.com 2019 - 2024. All rights reserved.