为什么我的if语句出乎意料?

2024-04-20 04:33:33 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的代码,我要创建一个列表,其中包含应该有距离的地方。用户应该选择两个不同的地方来查看它们之间的距离(以英里为单位),然后可以选择转换为公里。不过,我完成了代码,所以它工作了,然后我去添加一个if语句,应该说“如果用户输入place1,place2或place3,然后继续要求用户输入第二个位置,否则重新启动程序回到开始。但是if语句写得不正确,现在它说“Second未定义”

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'

Tags: to用户frominputrawifselectare
1条回答
网友
1楼 · 发布于 2024-04-20 04:33:33

不要缩进你的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'    

相关问题 更多 >