简体   繁体   中英

Total x the price is not working - Python 3.x

cheems

在此处输入图像描述

problemsencountered

在此处输入图像描述

Hello. I'm trying to do a mini business management using class, objects etc. What im trying to do is, whenever I input quantity of the items, it will multiply to the price and will get a total after I input something in 3 items.(Sticker, shirt and shoes) I tried to change it every time I got an error but still getting a new error after changing. I got list not callable or not subscriptable after changing things. I also got an error "Student not defined" before even tho the class is named "Student". Im also having errors in line 25 and 13 which I think I input correctly. Please see the attached photos/code for reference. I'm going to use this code with tkinter. Thank you in advance.

class Student():
    def __init__ (self):
        self.sticker =[]
        self.shirt = []
        self.shoes = []

    def getData(self, m1,m2, m3):
        self.sticker.append(m1)
        self.shirt.append(m2)
        self.shoes.append(m3)

    def displayData(self):
        return('Total sales are:' + self.average())
    def total(self):
        return(self.sticker[0]*5, self.shirt[1]*200, self.shoes[2]*1000)

    def average (self):
        return(self.sticker[0] + self.shirt[1] + self.shoes[2])
m1 = (input('Enter the quantity of sticker sold: '))
m2 = (input('Enter the quantity of shirt sold: '))
m3 = (input('Enter the quantity of the shoes sold: '))

s1 = Student()
s1.getData(m1, m2, m3)
s1.displayData()

And the error:

Traceback (most recent call last):
  File "c:\Users\Kenken\Desktop\Kenken - Python\businesshit.py", line 25, in <module>   
    s1.displayData()
  File "c:\Users\Kenken\Desktop\Kenken - Python\businesshit.py", line 14, in displayData
    print('Total sales are:' + self.total())
  File "c:\Users\Kenken\Desktop\Kenken - Python\businesshit.py", line 16, in total
    return(self.sticker[0]*5, self.shirt[1]*200, self.shoes[2]*1000)
IndexError: list index out of range
class Student():
    def __init__(self):
        self.sticker = []
        self.shirt=[]
        self.shoes=[]
    def getData(self, m1,m2, m3):
        self.sticker.append(m1)
        self.shirt.append(m2)
        self.shoes.append(m3)
    def displayData(self):
        return('Total sales are:' + self.average())
    def total(self):
        return(self.sticker[0]*5, self.shirt[0]*200, self.shoes [0]*1000)
    def average (self):
        return(self.sticker[0] + self.shirt[0] + self.shoes[0])
m1=(input('Enter the quantity of sticker sold: '))
m2=(input('Enter the quantity of shirt sold: '))
m3=(input('Enter the quantity of the shoes sold: '))
s1 = Student()
s1.getData(m1, m2, m3)
s1.displayData()

You should give a try to this code.

There are two mistakes in your code:

1)You should write all the methods in a class with same indentation.But in your code the method average was not aligned with other methods.

2)You appended the elements into empty list so their index was at position 0.so in total and average methods in Student class you need to use the index 0 while accessing elements in shirts and shoes as shirts[0] and shoes[0]

In order to print the total value use print(s1.displayData())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM