舍入一个str并在int中使用的float

时间:2015-10-05 00:19:25

标签: python

我试图复制我在类似Scratch的应用程序上制作的程序。我的问题是我试图向用户显示不同的数字数据(小计,税金和总成本),但是当它显示总成本时,它会给出重复或终止小数。我希望它四舍五入到小数点后两位。我已尝试在程序中添加 round() 命令,但这很困难,因为我试图对变量进行舍入而不是实际数字。这是我的代码到目前为止(我认为第25和28行是我想要添加 round() 或新行的地方)。我是Python的新手,我使用的是3.5.0版本。我也在这里搜索过,但答案对我来说太复杂了。另外,在我认为无法工作的地方添加error: typeerror type str doesn't define __round__ method功能时,我得到了 round() 。谢谢。 (在最后else:语句后忽略)

#This program asks for the size of pizza and how many toppings the customer would like and calculates the subtotal, tax and total cost of the pizza.
largePizza = 'large'
extraLargePizza = 'extra large'
print ('Answer the follwing question in all lowercase letters.')
print ('Would you like a large or an extra large pizza?')
sizeOfPizza = input()
if sizeOfPizza == largePizza:
    print('Large pizza. Good choice!')
else:
    print('Extra large pizza. Good choice!')
costOfLargePizza = str(6)
costOfExtraLargePizza = str(10)
oneTopping = 'one'
twoToppings = 'two'
threeToppings = 'three'
fourToppings = 'four'
print ('Answer the following question using words. (one, two, three, four)')
print ('How many toppings would you like on your pizza?')
numberOfToppings = input()
tax = '13%'
if numberOfToppings == oneTopping:
    print('One topping, okay.')
    if sizeOfPizza == largePizza:
        subtotalCostOfLargePizza = str(int(costOfLargePizza) + 1)
        **totalCostOfLargePizza = str(int(subtotalCostOfLargePizza) * 1.13)**
        print('Your subtotal cost is ' + str(subtotalCostOfLargePizza))
        print('Your tax is ' + str(tax))
        **print('Your total cost is ' + str(totalCostOfLargePizza))**
    else:
        print('One topping, okay.')
        subtotalCostOfExtraLargePizza = str(int(costOfExtraLargePizza) + 1)
        totalCostOfExtraLargePizza = str(int(subtotalCostOfExtraLargePizza) * 1.13)
        print('Your subtotal cost is ' + str(subtotalCostOfExtraLargePizza))
        print('Your tax is ' + str(tax))
        print('Your total cost is ' + str(totalCostOfExtraLargePizza))

0 个答案:

没有答案