输入后程序崩溃

时间:2019-07-31 15:25:14

标签: python python-3.x input while-loop

用户输入后未报告任何错误,程序崩溃,而没有推进其余功能。

def request1():
    responses1 = ["BFR", "Blackfriars", "London Blackfriars", "blackfriars"]
    dep = None
    while dep is None:
        try:
            dep = input(" Where are you travelling from? :")
            dep = str(dep)
        except dep not in responses1:
            print(" Location of departure invalid...")
            continue
        else:   
            real_dep ="BFR"
            print(" Location of departure determined...")
            break
    print(real_dep)

def request2():
    responses2 = ["HSK", "Hassocks", "hassocks"]
    while True:
        try:
            ariv = input(" Where are you travelling to? :")
            ariv = str(ariv)
        except ariv not in responses2:
            print(" Location of arrival invalid...")
            continue
        else:
            real_ariv ="HSK"
            print(" Location of arrival determined... \n Now processing results...")
            break
    print(real_ariv)    

request1()

request2()

请告知导致此错误的原因以及如何解决此错误,以便可以开发和使用此模块。

1 个答案:

答案 0 :(得分:0)

运行脚本不会显示任何错误,您能否提供有关程序崩溃时的更多信息?如其他用户所问,您如何运行该模块?

我删除了列表中的小写字母,以便可以在if-else子句中对其进行检查。

def request1():
    responses1 = ["BFR", "Blackfriars", "London Blackfriars"]
    while True:
        try:
            dep = input(" Where are you travelling from? :")
            if dep.lower() not in [string.lower() for string in responses1]:
                print(" Location of departure invalid...")
                continue
            else: 
                real_dep ="BFR"  
                print(" Location of departure determined...")
                print(real_dep)
                break
        except:
            print("Failed")

def request2():
    responses2 = ["HSK", "Hassocks"]
    while True:
        try:
            ariv = input(" Where are you travelling to? :")
            if ariv.lower() not in [string.lower() for string in responses2]:
                print(" Location of arrival invalid...")
                continue
            else:
                real_ariv ="HSK"
                print(" Location of arrival determined... \n Now processing results...")
                print(real_ariv) 
                break
        except:
            print("Failed")

request1()

request2()
相关问题