Can't multiply sequence by non-int of type 'float' in Python -
i have following code in 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 = $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 variable because wont go without , idk else do>.< totalcost.menu(display) while size!="a" , size!="b" , size!="c" , size!="d" , size!="e": size=str(input("which size? please enter a,b,c,d, or e. : ")) quantity=int(input("how many of size? : ")) while quantity<0: quantity=int(input("how many of 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()
i receive following error when execute code:
file "c:/python33/halpmeanon.py", line 21, in determinetotal self._total= self._cost * self.__quantity typeerror: can't multiply sequence non-int of type 'float'
context
this program supposed ask letter(size) & quantity, determine cost per unit given letter, , calculate/output total cost.
how can resolve error in code?
you got order of arguments wrong way round in
calc=totalcost(size, quantity)
your constructor is:
def __init__(self, quantity, size):
a great way code can make sure doesn't happen name arguments when calling method:
instead of:
calc=totalcost(size, quantity)
do this:
calc=totalcost(size=size, quantity=quantity) # or totalcost(quantity=quantity, size=size)
that way can give arguments out of order , not have worry bugs 1 encountered.
Comments
Post a Comment