【问题标题】:Can't multiply sequence by non-int of type 'float' in Python不能在 Python 中将序列乘以“浮点”类型的非整数
【发布时间】:2013-05-09 18:15:22
【问题描述】:

我在 python 中有以下代码:

class TotalCost:
      #constructor
      def __init__(self, quantity, size):
       self.__quantity=quantity
       self.__size=size
       self.__cost=0.0
       self.__total=0.0
      def DetermineCost(self):
        #determine cost per unit
       if self.__size=="A":
           self.__cost=2.29
       elif self.__size=="B":
           self.__cost=3.50
       elif self.__size=="C":
           self.__cost=4.95
       elif self.__size=="D":
           self.__cost=7.00
       elif self.__size=="E":
           self.__cost=9.95
    def DetermineTotal(self): #calculate total
       self.__total= self.__cost * self.__quantity
    def GetCost(self):
       return self.__cost
      def GetTotal(self):
       return self.__total
      def Menu(self):
       print("----------------SIZES/PRICES----------------")
       print("               Size A = $2.92")
       print("               Size B = $3.50")
       print("               Size C = $4.95")
       print("               Size D = $7.00")
       print("               Size E = $9.95")
       print("--------------------------------------------")
    def main():
     again=""
     print("Prices:")
     while again!="no":
        size=""
        quantity=0
        display="" #i put this variable only because it wont go without it and idk what else to do>.<
        TotalCost.Menu(display)
        while size!="A" and size!="B" and size!="C" and size!="D" and size!="E":
            size=str(input("Which size? Please enter A,B,C,D, or E. : "))
        quantity=int(input("How many of this size? : "))
        while quantity<0:
            quantity=int(input("How many of this size? : "))
        Calc=TotalCost(size, quantity)  
        Calc.DetermineCost()
        Calc.DetermineTotal()
        print("--------------------------------------------")
        print("Your order:")
        print("Size: " , size)
        print("Quantity: " , quantity)
        print("Cost each: $" , Calc.GetCost())        print("Total cost: $", Calc.GetTotal())

    main()  

执行此代码时收到以下错误:

DetermineTotal 中的文件“C:/Python33/halpmeanon.py”第 21 行 self._total=self._cost * self.__quantity TypeError:不能将序列乘以“浮点”类型的非整数

上下文

这个程序应该要求一个字母(尺寸)和数量,通过给定的字母确定每单位的成本,并计算/输出总成本。

如何解决我的代码中的这个错误?

【问题讨论】:

  • 就在self.__total= self.__cost * self.__quantity 之前,你能做一个print self.__cost, self.__quantity 吗?
  • @tinasucksatpython 我试图改善你的问题。我们不关心您是否是“新手”,我们只关心您的问题内容、您的努力、拼写和语法。在 Stack Overflow 上提问时请记住这一点。
  • 对不起,这里没有无关紧要的形容词,srs business。 [真诚地为讽刺道歉,再次感谢你们]

标签: python


【解决方案1】:

你弄错了参数的顺序

Calc=TotalCost(size, quantity)  

你的构造函数是:

  def __init__(self, quantity, size):

一个很好的编码方法是在调用方法时命名你的参数,这样你就可以确保不会发生这种情况:

代替:

Calc=TotalCost(size, quantity)

这样做:

Calc=TotalCost(size=size, quantity=quantity) # or TotalCost(quantity=quantity, size=size)

这样您就可以无序地给出参数,而不必担心遇到的错误。

【讨论】:

  • 谢谢,我知道这一定是个愚蠢的小东西,我爱你。 :)
猜你喜欢
  • 1970-01-01
  • 2015-06-27
  • 2011-04-06
  • 2012-09-17
  • 1970-01-01
  • 2018-12-19
  • 2013-09-11
相关资源
最近更新 更多