Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我需要帮助修复str方法。不归还任何东西_Python_Class - Fatal编程技术网

Python 我需要帮助修复str方法。不归还任何东西

Python 我需要帮助修复str方法。不归还任何东西,python,class,Python,Class,我不太确定str方法是如何工作的。我希望它返回以下字符串。“计算机有一个cpu在(var)运行”,在第一行,在第二行,它需要跟随。。。“它有(var)GB RAM和(var)GB可用存储空间。 以下是一个示例输出: 计算机的cpu运行频率为3.1 Ghz 它有16.0 GB RAM和2000.0可用存储空间 以下是我返回时遇到的问题代码 def __str__(self): computer_str = Computer.__str__(self), print("The com

我不太确定str方法是如何工作的。我希望它返回以下字符串。“计算机有一个cpu在(var)运行”,在第一行,在第二行,它需要跟随。。。“它有(var)GB RAM和(var)GB可用存储空间。 以下是一个示例输出:

计算机的cpu运行频率为3.1 Ghz

它有16.0 GB RAM和2000.0可用存储空间

以下是我返回时遇到的问题代码

def __str__(self):
        computer_str = Computer.__str__(self), print("The computer has a cpu running at " + cpu + "Ghz")
        computer_str += Computer.__str__(self), print("It has " + ram + "GB RAM and has " + drive + " GB of storage available")
        return computer_str
如果有帮助,下面是完整的代码

class Computer:
    def __init__(self, cpu, ram, drive):
        self.__cpu = 0
        self.__ram = 0
        self.__drive = 0

    def set_cpu(self, speed):
        self.__cpu = speed

    def get_cpu(self):
        return self.__cpu

    def set_ram(self, size):
        self.__ram = size

    def get_ram(self):
        return self.__ram

    def set_drive(self, size):
        self.__drive = size

    def get_drive(self):
        return self.__drive

    def __str__(self):
        computer_str = Computer.__str__(self), print("The computer has a cpu running at " + cpu + "Ghz")
        computer_str += Computer.__str__(self), print("It has " + ram + "GB RAM and has " + drive + " GB of storage available")
        return computer_str


print("Describe your computer")
print()
cpu = input("How fast is your computer in GHz?: ")
ram = input("How much RAM does your computer have in GB? : ")
drive = input("How much drive space does your computer have in GB?: ")
computer1 = Computer(cpu, ram, drive)

您不应该在
\uuuuu str\uuuu
函数中打印。另外,不确定为什么要在
\uuuu str\uuuu
函数中递归调用
\uuuuuu str\uuuuu
,这将变成一个无休止的循环

您想要的是:

def __str__(self):
    computer_str = "The computer has a cpu running at " + cpu + "Ghz \n"
    computer_str += "It has " + ram + "GB RAM and has " + drive + " GB of storage available"
    return computer_str
当您在
computer1
上调用
print
时,它将打印上述字符串。
\n
将使其显示在两行上

现在,上述代码将不起作用,因为您没有正确访问
cpu
驱动器
、和
ram
变量,但这是一个单独的问题


实例化对象时,您也没有设置这些变量,但这也是另一个问题

您不应该在
\uuuuu str\uuuu
函数中打印。此外,不确定为什么要在
\uuuuu str\uuuuu
函数中递归调用
\uuuuu str\uuuuu
,这将变成一个无休止的循环

您想要的是:

def __str__(self):
    computer_str = "The computer has a cpu running at " + cpu + "Ghz \n"
    computer_str += "It has " + ram + "GB RAM and has " + drive + " GB of storage available"
    return computer_str
当您在
computer1
上调用
print
时,它将打印上述字符串。
\n
将使其显示在两行上

现在,上述代码将不起作用,因为您没有正确访问
cpu
驱动器
、和
ram
变量,但这是一个单独的问题


实例化对象时,您也没有设置这些变量,但这也是另一个问题

返回的错误是什么?返回的错误是什么?应该是
\uuuuu self.cpu
\uuuuu self.ram
,和
\uuuu self.drive
。编辑:添加了前导
\uuuuuuu
@chepnerWell,
self.cpu
等例如,给定类的当前状态。它应该是
self.cpu
,假设所有样板文件都不是赋值的无意义要求。是的,他们实际上需要在
\uuuuu init\uuuuuu
中设置这些变量,而不是将它们设置为0。或者调用setter方法应该是
\uuu self.cpu
\uu self.ram
self.drive
.Edit:添加了前导
\uuu
@chepnerWell,
self.\uu-cpu
等,给定类的当前状态。它应该是
self.cpu
,假设所有样板文件不是赋值的无意义要求。是的,他们实际上需要在
\uini中设置这些变量t_u
,而不是将它们设置为0。或者调用setter方法