不要缩进你的 if 语句。
First = int(input('Please select from the list where you are coming from'))
if answer in ['place1', 'place2', 'place3']:
Second = int(input('Please select where you are going to: '))
else:
print 'please press any key to restart'
代替
First = int(input('Please select from the list where you are coming from'))
if answer in ['place1', 'place2', 'place3']:
Second = int(input('Please select where you are going to: '))
else:
print 'please press any key to restart'
编辑:
看来你需要一些帮助。我重新编写了您的代码并添加了一些评论,希望能把事情弄清楚。
#using a dictionary allows us to associate, or hash, a string with a value
place_dict = {"place1":50,"place2":40,"place3":30}
convert = 1.609344
def run_main():
#placing the bulk of the program inside of a function allows for easy restarting
print 'The available places to select from are:'
print 'place1, place2, place3: '
first = raw_input('Please select from the list where you are coming from: ')
second = raw_input('Please select where you are going: ')
#the values the user puts in are now stored in variables called 'first' and 'second', so if the user inputs "one", then first == "one"
if first not in place_dict or second not in place_dict:
#check to ensure that both first and second are in the place place dictionary
#if not, then return none to the main program
raw_input('Press any key to restart...')
return None
#an else is not needed here, because if the if statement executes, then we will not reach this point in the program
#this returns the dictionary value of the first value that the user inputs + the second.
return place_dict[first] + place_dict[second]
if __name__ == "__main__":
#this line says that if we are running the program, then to execute. This is to guard against unwanted behavior when importing
distance = None
#set an initial variable to None
while distance is None:
#run the main program until we get an actual value for distance
distance = run_main()
print "the distance between the two places are", distance, "miles"
kilometres = bool(input('Would you like to convert to Kilometres? please type True to convert or False to exit: '))
if kilometres == True:
print 'The distance in Kilometres is',distance / convert, 'Kilometres'
else:
print 'press any key to terminate'