-3

所以下面是我的代码,我应该创建一个列表,其中包含应该有距离的地方。用户应该选择两个不同的地方以查看它们之间的距离(以英里为单位),然后可以选择将它们转换为公里。然而,我完成了代码,所以它工作了,然后我去添加一个 if 语句,应该说“如果用户输入地点 1、地点 2 或地点 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'
4

1 回答 1

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'    
于 2013-07-14T21:22:25.153 回答