可以教python吗

为什么答案都是 not order this food?

def find_food(self, name):
if name not in self.foods:
print("This customer do not order this food!")
return
idx = self.foods.index(name)
print('{} is in the page {} '.format(name, idx))

return 不应该放在那里,return具有“打断函数”的作用。只要出现了return,并且return执行了,那么这个函数在return语句之后的所有语句都不会执行。这里你的缩进我不确定,有可能是这个return是必然执行的,所以不管怎么样,都会print not order,然后就跳出了

可以改成这样

def find_food(self, name):
    if name not in self.foods:
        print("This customer do not order this food!")
    else:
        idx = self.foods.index(name)
        print('{} is in the page {} '.format(name, idx))
        #return