类不打印整个变量

时间:2019-02-19 18:56:59

标签: python class

我不是编码新手,所以我一直在尝试弄清楚如何获取整个输出变量以打印到终端。现在,当我使用Change maker实例作为gregs_vending_machine打印self.output时,将打印前5种方法。

注意:四舍五入方法将使我的数字在丢掉一分钱时都被丢弃,所以我的计划是将应付的零钱乘以100,然后从那去。

一旦我解决了上面的方法,我将使用一种将全局值字典用于我的值的方法进行重构来解决。

代码在下面:

   class ChangeMaker:
    def __init__(self, total_price, amount_paid):
        self.total_price = total_price
        self.amount_paid = amount_paid
        self.change_due= self.change(self.total_price,self.amount_paid)
        self.output = (f'For an item that costs ${self.total_price} with an amount paid of ${self.amount_paid} your change due is ${self.change_due}. Given in{self.hundreds_sentence_generator()}{self.fifties_sentence_generator()}{self.twenties_sentence_generator()}{self.tens_sentence_generator()}{self.fives_sentence_generator()}{self.dollars_sentence_generator()}\n{self.quarters_sentence_generator()}{self.dimes_sentence_generator()}{self.nickels_sentence_generator()}{self.pennies_sentence_generator()}')
        self.change_output = (f'{self.quarters_sentence_generator()}{self.dimes_sentence_generator()}{self.nickels_sentence_generator()}{self.pennies_sentence_generator()}') def change(self, total_price, amount_paid):
        return round(int(amount_paid) - int(total_price),2)

    def hundreds_sentence_generator(self):
      hundreds_due= self.change_due//round(100.00,2)
      self.change_due -= round(100.00,2) * hundreds_due
      if hundreds_due == 1:
        return f' {int(hundreds_due)}: $100 Bill, '
      elif hundreds_due > 1:
        return f' {int(hundreds_due)}: $100 Bills, '
      else:
        return ""


    def fifties_sentence_generator(self):
      fifties_due= self.change_due//round(50.00,2)
      self.change_due -= round(50.00,2) * fifties_due
      if fifties_due == 1:
        return f'{int(fifties_due)}: $50 Bill, '
      elif fifties_due > 1:
        return f'{int(fifties_due)}: $50 Bills, '
      else:
        return ""

    def twenties_sentence_generator(self):
      twenties_due= self.change_due//round(20.00,2)
      self.change_due -= round(20.00,2) * twenties_due
      if twenties_due == 1:
        return f'{int(twenties_due)}: $20 Bill, '
      elif twenties_due > 1:
        return f'{int(twenties_due)}: $20 Bills, '
      else:
        return ""

    def tens_sentence_generator(self):
      tens_due= self.change_due//round(10.00,2)
      self.change_due -= round(10.00,2) * tens_due
      if tens_due == 1:
        return f'{int(tens_due)}: $10 Bill, '
      elif tens_due > 1:
        return f'{int(tens_due)}: $10 Bills, '
      else:
        return ""

    def fives_sentence_generator(self):
      fives_due= self.change_due//round(5.00,2)
      self.change_due -= round(5.00,2) * fives_due
      if fives_due == 1:
        return f'{int(fives_due)}: $5 Bill, '
      else:
        return ""

    def dollars_sentence_generator(self):
      dollars_due= self.change_due//round(1.00,2)
      self.change_due -= round(1.00,2) * dollars_due
      if dollars_due == 1:
        return f'{int(dollars_due)}: $1 Bill, '
      elif dollars_due > 1:
        return f'{int(dollars_due)}: dollars, '
      else:
        return ""

    def quarters_sentence_generator(self):
      quarters_due= self.change_due//round(0.25,2)
      self.change_due -= round(0.25,2) * quarters_due
      if quarters_due == 1:
        return f'{int(quarters_due)}: Quarter, '
      elif quarters_due > 1:
        return f'{int(quarters_due)}: Quarters, '
      else:
        return ""

    def dimes_sentence_generator(self):
      dimes_due= self.change_due//round(0.10,2)
      self.change_due -= round(0.10,2) * dimes_due
      if dimes_due == 1:
        return f'{int(dimes_due)}: Dime, '
      elif dimes_due > 1:
        return f'{int(dimes_due)}: Dimes, '
      else:
        return ""

    def nickels_sentence_generator(self):
      nickels_due= self.change_due//round(0.05,4)
      self.change_due -= round(0.05,2) * nickels_due
      if nickels_due == 1:
        return f'{int(nickels_due)}: Nickel, '
      else:
        return ""

    def pennies_sentence_generator(self):
      if self.change_due == round(0.01,2):
        return f'1: Penny, '
      elif self.change_due > round(0.01,2):
        return f'{int(self.change_due*100)}: Pennies, '
      else:
        return ""

gregs_vending_machine = ChangeMaker(1.01, 190.88)
print(gregs_vending_machine.output)''

2 个答案:

答案 0 :(得分:1)

我认为您的问题可能是您将amount_paidtotal_price转换为整数。这将导致小数点被截断。如果将您的change方法更改为以下方法,我似乎会得到正确的结果。

def change(self, total_price, amount_paid):
    return round(amount_paid - total_price, 2)

答案 1 :(得分:0)

您的self.change()方法正在使用int,因此您的change_due将永远不会包含任何硬币。因此,只需将您的self.change()更改为:

def change(self, total_price, amount_paid):
    return amount_paid - total_price

我认为这一切都会起作用。