与inpu有关的Python问题

2024-05-20 02:44:48 发布

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

**我使用的代码是python,我试图输入一个double true,并得到一个数字输出

def get\ U报警(提示):

TT=0.95

TF=0.94

英尺=0.29

FF=0.001 如果为真:

尝试: 返回

{“True,True”:TT,“True,False”:TF,“False,True”:FT,“False,False”:FF}[输入(提示).lower()]

        return 

{“true,true”:TT,“true,false”:TF,“false,true”:FT,“false,false”:FF}[输入(提示).lower()]

除KeyError外:

print(“无效输入,请输入true和,或False!”)**你知道吗

def get\u EarthQ(提示):

a=0.002

b=0.998

如果为真:

尝试: return{“true”:a,“false”:b}[输入(提示).lower()]

return{“True”:a,“False”:b}[输入(提示).lower()]

除KeyError外:

print(“无效输入,请输入True或False!”)你知道吗

def get\ U Burg(提示):

a=0.001

b=0.999

如果为真:

尝试:

return{“true”:a,“false”:b}[输入(提示).lower()]

return{“True”:a,“False”:b}[输入(提示).lower()]

除KeyError外: print(“无效输入,请输入True或False!”)你知道吗

打印(“报警系统”)

打印(get_Burg(“可能是burlar闯入了家吗?”)你知道吗

打印(get\u EarthQ(“你的地震答案是真是假?”)你知道吗

print(“burgerly或地震或两者都发出警报的概率是多少”)

打印(get\u Alarm(“Add true,true例如to the get the probability,Enter?”))你知道吗

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


Tags: falsetrue报警getreturntfdeflower
1条回答
网友
1楼 · 发布于 2024-05-20 02:44:48

看看这里的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将不会被访问

相关问题 更多 >