简体   繁体   中英

too many arguments for __init__()

I was trying to write a program, in which a number, that represents a quantity of objects is inputted. Here, the objects are people, PCs and phones. The program needs to assign a PC and a phone to every person and then output the specs of these devices for every person. I get a " init () takes 4 positional arguments but 6 were given" error when i try to input a number of PCs. The program is divided into 3 files: main, PC and Person. main:

from PC import PC
from Person import Human
from PC import phone
phones = []
pcs = [] 

numberofphones = int(input())

for i in range(0,numberofphones):
    display = str(input("Display: "))
    cpu = str(input("CPU: "))
    gpu = str(input("GPU: "))
    ram = str(input("RAM: "))
    camera = str(input("Camera: ")) 
    model = str(input("Model: "))
    a = phone(display, cpu, gpu, ram, camera, model)
    print("Phone specifications: ")
    a.show_phone()
    phones.append(a)


print(phone)

numberofpcs = int(input())

for j in range(0,numberofpcs):
    ram1 = str(input("RAM: "))
    cpu1 = str(input("CPU: "))
    gpu1 = str(input("GPU: "))
    psu = str(input("PSU capacity: "))
    hdd = str(input("HDD capacity: ")) 
    pc1 = PC(ram1, cpu1, gpu1, psu, hdd)
    print("PC specifications: ")
    pc1.show_pc()
    pcs.append(pc1)

print(pcs) 

PC:

class PC:
    def __init__(self, ram, cpu, gpu):
        self.ram = ram
        self.cpu = cpu
        self.gpu = gpu

    def show_pc(self):
        print("RAM: " + str(self.ram))
        print("CPU: " + str(self.cpu))
        print("GPU: " + str(self.gpu))

class phone:
    def __init__(self, display, cpu, gpu, ram, camera, model):
        self.display = display
        self.cpu = cpu
        self.gpu = gpu
        self.ram = ram
        self.camera = camera
        self.model = model

    def show_phone(self):
        print("Display: " + str(self.display))
        print("CPU: " + str(self.cpu))
        print("GPU: " + str(self.gpu))
        print("RAM: " + str(self.ram))
        print("Camera: " + str(self.camera))
        print("Model: " + str(self.model))

Person:

class Human:
    def __init__(self, name, age,gender,height, pc,ph):
        self.name = name
        self.age = age
        self.pc = pc
        self.ph = ph
        self.gender = gender
        self.height = height





    def show_human(self):
        print("Name: " + self.name)
        print("Age: " + str(self.age))
        print("Gender: " + self.gender)
        print("Height: " + str(self.height))
        print('PC')
        self.pc.show_pc()
        print("Phone")
        self.ph.show_phone()

I tried so many things so a bit of help would be really appreciated!

This line in main is the culprit:

pc1 = PC(ram1, cpu1, gpu1, psu, hdd)

You are passing in psu and hdd but the init function of PC does not take in those extra parameters.

class PC:
    def __init__(self, ram, cpu, gpu):
        self.ram = ram
        self.cpu = cpu
        self.gpu = gpu

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