Python问题(类,超基本)

2024-10-02 10:31:38 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在玩弄python类,我很难理解它,这就是我想到的

class Forest:
    def temperature (self,temperature):
        self.temperature=temperature
    def displayTemp (self):
        return self.temperature
    def saying(self):
        print "This soup is %s" % self.displayTemp  

first=Forest()
second=Forest()
third=Forest()

first.temperature('too cold.')
second.temperature('warm.')
third.temperature('too hot.')

temperature=input("1~10; How hot is the soup? ")
int(temperature)

if (temperature<=3):
    first.saying
elif (temperature==4,5,6):
    second.saying
else:
    third.saying

它的目标是问一个像"1~10; How hot is the soup? "这样的问题,然后输入一个数字(1-3=太冷;4-6太热;7-10太热),但是它不回叫回答,而是什么也不做。你知道吗

我对python完全陌生,我环顾了四周,但是我对python的缺乏理解让我很难理解,我在这里读了一些我认为可能是的东西,但是没有运气。你知道吗


Tags: theselfisdefhowtoofirstsecond
1条回答
网友
1楼 · 发布于 2024-10-02 10:31:38

你的代码有几个问题。你知道吗

本身first.saying只是对函数对象的引用;实际调用它需要括号:first.saying()second.saying(),等等。对self.displayTemp()也需要这样做。你知道吗

此外,int(temperature)不会更改temperature的值;它返回一个新的值,该值被转换为int。如果要替换旧值,则需要执行temperature = int(temperature)。你知道吗

而且temperature==4,5,6不会做你想做的事。你需要做一些类似4 <= temperature <= 6的事情。你知道吗

您应该阅读the Python tutorial以熟悉Python的基础知识。你知道吗

相关问题 更多 >

    热门问题