python - 输入的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 时,我陷入了一个循环我想知道我做错了什么
解决方案
在这里查看您的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
将不会被访问
推荐阅读
- python - Python:如何在 1 行中从列表中打印字符串和数字?
- java - 使用 FCM、SQL、java 向多个设备发送推送通知
- ruby-on-rails - Rails 重定向操作:索引,不起作用
- javascript - 使用 JavaScript 打开 Chrome PDF 查看器对话框
- java - 试图让一个摆动菜单与 GridBagLayout 一起工作
- angular - Angular Router URL 编码特殊字符和浏览器行为
- ajax - 如何避免 Ajax 调用中的循环
- ruby-on-rails - 当我使用“ModelName.all”时,ActiveRecord 没有返回表中的所有值
- json - 使用 NodeJs 将 Json 文件转换为 Markdown
- session - 从 MVC Core 中的数据库表中获取会话数据