所以我在玩弄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的缺乏理解让我很难理解,我在这里读了一些我认为可能是的东西,但是没有运气。你知道吗
你的代码有几个问题。你知道吗
本身
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的基础知识。你知道吗
相关问题 更多 >
编程相关推荐