这是我遇到问题的代码,它只在有状态时才有效。我需要它在有状态放入状态和状态和城市时才能工作。真的全部我需要有人来帮助我修改变量。
import urllib2
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
capitals = {'Alabama': 'Montgomery',
'Alaska': 'Juneau',
'Arizona': 'Phoenix',
'Arkansas': 'Little Rock',
'California': 'Sacramento',
'Colorado': 'Denver',
'Connecticut': 'Hartford',
'Delaware': 'Dover',
'Florida': 'Tallahassee',
'Georgia': 'Atlanta',
'Hawaii': 'Honolulu',
'Idaho': 'Boise',
'Illinois': 'Springfield',
'Indiana': 'Indianapolis',
'Iowa': 'Des Moines',
'Kansas': 'Topeka',
'Kentucky': 'Frankfort',
'Louisiana': 'Baton Rouge',
'Maine': 'Augusta',
'Maryland': 'Annapolis',
'Massachusetts': 'Boston',
'Michigan': 'Lansing',
'Minnesota': 'St. Paul',
'Mississippi': 'Jackson',
'Missouri': 'Jefferson City',
'Montana': 'Helena',
'Nebraska': 'Lincoln',
'Nevada': 'Carson City',
'New Hampshire': 'Concord',
'New Jersey': 'Trenton',
'New Mexico': 'Santa Fe',
'New York': 'Albany',
'North Carolina': 'Raleigh',
'North Dakota': 'Bismark',
'Ohio': 'Columbus',
'Oklahoma': 'Oklahoma City',
'Oregon': 'Salem',
'Pennsylvania': 'Harrisburg',
'Rhode Island': 'Providence',
'South Carolina': 'Columbia',
'South Dakota': 'Pierre',
'Tennessee': 'Nashville',
'Texas': 'Austin',
'Utah': 'Salt Lake City',
'Vermont': 'Montpelier',
'Virgina': 'Richmond',
'Washington': 'Olympia',
'West Virgina': 'Charleston',
'Wisconsin': 'Madison',
'Wyoming': 'Cheyenne'}
states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida',
'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska',
'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio',
'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas',
'Utah', 'Vermont', 'Virgina', 'Washington','West Virgina', 'Wisconsin', 'Wyoming']
def make_word(words):
result = ""
for i in words:
result += i
return result
while 1:
a = raw_input('Put in a place: ')
a = a.lower()
y = a.replace(' ', '%20')
a = list(a)
a[0] = a[0].upper()
a = ''.join(a)
num = 0
cap = 0
for key in capitals:
if key == a:
page = urllib2.urlopen('http://woeid.rosselliot.co.nz/lookup/%s' % capitals[key]).read()
cap = capitals[key]
num += 1
if a in states:
f = page.find(cap)
if f != -1:
start = page.find('data-center_long="', f) + 18
end = page.find('"', start)
start1 = page.find('data-center_lat="', end) + 17
end1 = page.find('"', start1)
print '%s Latitude: %s Longitude: %s' % (a, page[start1:end1], page[start:end])
else:
for b in states:
l = len(b) + 1
f = a.find(b)
if f != -1:
f = len(a[f:])
f = len(a) - f
f = a[:f]
page = urllib2.urlopen('http://woeid.rosselliot.co.nz/lookup/%s' % f).read()
start = page.find('data-center_long="', f) + 18
end = page.find('"', start)
start1 = page.find('data-center_lat="', end) + 17
end1 = page.find('"', start1)
print '%s Latitude: %s Longitude: %s' % (a, page[start1:end1], page[start:end])
这表示变量没有定义,但我需要代码保持这样,否则它很可能不会起作用。有人可以给我一个有用的提示或答案吗?
答案 0 :(得分:1)
f
变量在if a in states
内声明。因此,如果a in states
条件的计算结果为False,则不会定义f
。