只有最后一个elif块被执行

时间:2017-11-28 00:28:38

标签: python python-2.7

我暂时陷入一个看似微不足道的问题。

我有一个条件循环,我让用户输入location = raw_input("Where are you located? : ") #print (location) if location == "London": recreationList = londonRec businessList = londonBus elif location == "California": recreationList = caliRec businessList = caliBus elif location == "Mumbai": recreationList = mumbaiRec businessList = mumbaiBus ....code to be executed based on location 并根据输入的输入分配变量。

以下是解释清楚的代码:

raw_input()

我面临的问题是,当我输入mumbai作为"Where are you located?"时,以下代码才会被执行,每次它都会循环回{{1}}。

我觉得这是一件非常简单的事情,我完全忽略了,任何指导都很受欢迎!

由于

1 个答案:

答案 0 :(得分:2)

我认为你应该在为它们赋值之前创建变量。这可能会解决您的问题,我不确定。有关python范围的更多详细信息,请参阅此link。此外,您的缩进是不正确的。 试试这段代码:

location = raw_input("Where are you located? : ")
#print (location)

recreationList = None
businessList = None

if location == "London":
    recreationList = londonRec
    businessList = londonBus

elif  location == "California":
  recreationList = caliRec
  businessList = caliBus

elif  location == "Mumbai":
  recreationList = mumbaiRec
  businessList = mumbaiBus

....code to be executed based on location