python - catch_error_yn 函数不起作用
问题描述
我不知道如何使 catch_error_yn 工作。有人可以解释为什么该功能不起作用以及如何解决它吗?
def catch_error_yn():
unvalid = True
while unvalid:
try:
response = "Yes"
response = "No"
unvalid = False
return response
except ValueError:
print("please only enter 'Yes' or 'No' with capitals on Y or N:")
def carpark():
print("Do you want a free parking space, please answer 'Yes' or 'No' with capital letters on Y or N")
response = catch_error_yn
print(response)
print("Welcome to Copington Adventure Theme Park's automated ticket system\nplease press any button to see the ticket prices.")
enter = input()
print("\nAdult tickets are £20 each \nChild tickets are £12 each \nSenior citizen tickets are £11 each")
carpark()
解决方案
代码中似乎有一个小错误,因为您没有将 传递input()
给catch_error_yn()
函数。
我正在对您的代码的意图做出一些假设,并在此处添加修改后的工作代码:
def catch_error_yn():
unvalid = True
while unvalid:
try:
#get input inside the function
response = input()
# check if it is valid if, not raise an error
if response not in ['Y', 'N'] :
raise ValueError()
return response
except ValueError:
print("please only enter 'Yes' or 'No' with capitals on Y or N:")
def carpark():
print("Do you want a free parking space, please answer 'Yes' or 'No' with capital letters on Y or N")
response = catch_error_yn()
print(response)
print("Welcome to Copington Adventure Theme Park's automated ticket system\nplease press any button to see the ticket prices.")
print("\nAdult tickets are £20 each \nChild tickets are £12 each \nSenior citizen tickets are £11 each")
carpark()
推荐阅读
- jquery - jQuery Tablesorter:排序和过滤可编辑列
- vue.js - 在 div vuejs 中添加 Html 元素
- mysql - 如何获得所有客户的前 3 个订单?
- html - Java Servlet 和 JSP 最初无法从会话中查看数组列表
- android - .KML 文件中的地标未显示在地图视图上
- php - 在同一台机器上运行 wamp (php 5.6) 和 xampp (7.2) 时,xampp 没有采用 php 7.2 版本,如何克服它?
- ruby-on-rails - Rails 中的多态 has_many_through
- python - 将 bbox_inches 设置为 'tight' 进行保存会更改显示坐标
- powerbi - 在 Power BI 中对堆栈条形图进行排序
- mysql - 如何使用 Doctrine Native Query 通过条件语句的连接计数对结果进行排序?