所以我在处理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完全陌生,我环顾四周,但是我缺乏理解使我变得更加困难,我在这里读了一些我认为可能是什么,但没有运气的东西。
发布于 2013-11-16 18:43:39
您的代码有几个问题。
first.saying本身只是对函数对象的引用;您需要括号才能真正调用它:first.saying()、second.saying()等等。您需要对self.displayTemp()做同样的操作。
而且,int(temperature)不改变temperature的值;它返回一个转换为int的新值。如果要替换旧值,则需要执行temperature = int(temperature)。
而且,temperature==4,5,6不会做你想做的事。你需要做一些像4 <= temperature <= 6这样的事情。
您应该阅读Python教程以熟悉Python的基础知识。
https://stackoverflow.com/questions/20022242
复制相似问题