首页 > 解决方案 > 输入的Python问题

问题描述

**我正在使用的代码是在 python 中,我尝试输入一个 double true,并获得一个数字输出

def get_Alarm(提示):

TT=0.95

TF=0.94

英尺=0.29

FF=0.001 时为真:

尝试:返回

{"True,True":TT,"True,False":TF,"False,True":FT,"False,False":FF}[input(prompt).lower()]

        return 

{"true,true":TT,"true,false":TF,"false,true":FT,"false,false":FF}[input(prompt).lower()]

除了 KeyError:

print("无效输入请输入真与,或假!")**

def get_EarthQ(提示):

a=0.002

b=0.998

而真:

尝试:返回{“真”:a,“假”:b}[输入(提示).lower()]

return {"True": a, "False": b}[input(prompt).lower()]

除了 KeyError:

print("输入无效,请输入真或假!")

def get_Burg(提示):

a=0.001

b=0.999

而真:

尝试:

返回{“真”:a,“假”:b}[输入(提示).lower()]

return {"True": a, "False": b}[input(prompt).lower()]

除了 KeyError: print("无效的输入,请输入 True 或 False!")

print("报警系统")

print(get_Burg("概率 burlar 闯入家门了吗?"))

print(get_EarthQ("你有地震的答案是真还是假?"))

print("从汉堡或地震或两者兼有的情况下发出警报的概率是多少")

print(get_Alarm("在获取概率的例子中添加 true, true, Enter?"))

当我为 getAlarm(prompt) 添加一个 true,true 时,我陷入了一个循环我想知道我做错了什么

标签: pythonpython-3.x

解决方案


在这里查看您的try陈述,它们是另一个input并且永远不会退出while True:循环,您需要一种方法来退出该while True:循环

def get_Alarm(prompt):
    TT=0.95
    TF=0.94
    FT=0.29
    FF=0.001
    while True:
        try:
            return {"True,True":TT,"True,False":TF,"False,True":FT,"False,False":FF}[input(prompt).lower()]

            return {"true,true":TT,"true,false":TF,"false,true":FT,"false,false":FF}[input(prompt).lower()]

        except KeyError:
            print("Invalid input please enter true and, or False!")

另请注意,一旦第一个return执行,第二个return将不会被访问


推荐阅读