以下是我的代码,我应该创建一个包含应该有距离的地方的列表。用户应选择两个不同的位置来查看它们在英里之间的距离,然后可以选择转换为公里。然而,我完成了代码,所以它工作,然后我去添加一个if语句,应该说“如果用户输入place1,place2或place 3然后继续要求用户输入第二个位置,否则重新启动程序回到开头。但if语句写得不正确,现在说“第二个没有定义”
print 'The available places to select from are:'
print 'place1, place2, place3: '
place1 = 50
place2 = 40
place3 = 30
Convert = 1.609344
First = str(raw_input('Please select from the list where you are coming from'))
if raw_input == 'place1' 'place2' 'place3':
Second = str(raw_input('Please select where you are going to: '))
else:
print 'please press any key to restart'
Distance = First + Second
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 True:
print 'The distance in Kilometres is',First + Second / Convert, 'Kilometres'
else:
print 'press any ket to terminate'
答案 0 :(得分:7)
不要缩进你的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'