在pyython 3方法定义中将变量设置为小数点后2位

时间:2018-08-16 08:13:33

标签: python-3.x

我有一个用python 3编写的作业。我们必须使用一些方法定义一个类,并将输入内容写入二进制文件。现在我已经编写了程序,它是99%正确的。我输入的值显示到小数点后2位只是个问题。我使用了round函数和%.2f,它一直显示到小数点后一位,这是我定义的类。

import pickle

class Shoe:

def __init__(self):
    self.style = ""
    self.price = 0.00

def assignValues(self, style, price):
    self.style = style
    self.price = price

def calcDiscountPrice(self, style):
    if self.style == "A" or self.style == "a":           
        self.priceD = price -(price / 10.00)
    elif self.style == "B" or self.style == "b":
        self.priceD = price - (price / 20.00)
    elif self.style == "O" or self.style == "o":
        self.priceD = price

def dispValues(self):      
    print("Shoe style:",self.style)
    print("Price: R ", self.price)
    print("Discounted price is R",self.priceD)
    print('\n')`

任何指导将不胜感激。

1 个答案:

答案 0 :(得分:0)

我知道了。 在dispValues中-> print("Price: R %.2f" %self.price) 在第二个%之前,我有一个逗号,这一直给我语法错误。 我的程序现在100%工作正常