Python问题(类,超基本)
所以我在搞python类,我很难理解它,这就是我想到的Python问题(类,超基本),python,class,python-2.7,Python,Class,Python 2.7,所以我在搞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.displayTem
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
类林:
def温度(自身、温度):
自身温度=温度
def displayTemp(自我):
返回自身温度
他说(自己):
打印“此汤为%s”%self.displayTemp
第一个=森林()
第二个=森林()
第三=森林()
首先,温度(“太冷”)
第二,温度(‘温暖’)
第三,温度(“太热”)
温度=输入(“1~10;汤有多热?”)
内部(温度)
如果(温度您的代码有几个问题
首先,它本身就是。saying
只是对函数对象的引用;您需要用括号来实际调用它:首先。saying()
,第二。saying()
,等等。您需要对self.displayTemp()
执行同样的操作
另外,int(temperature)
不会更改temperature
的值;它返回一个转换为int的新值。如果要替换旧值,需要执行temperature=int(temperature)
另外,temperature==4,5,6
也不会做你想做的事情。你需要做一些类似于4的比较,以x==4,5,6的形式做一些与预期不同的事情MG!非常感谢!我在newboston.org上看了这些视频,所以我想我已经很好地掌握了它,但我想有几件事从我身边溜走了,那就是非常感谢!temperature==4,5,6
相当于(temperature==4),5,6
,即一个非空元组,它始终是True
。