python - 在函数中引发 ValueError
问题描述
我已经编写了这段代码,当我提交给我的 MOOC 时,它会产生一个错误,但在我的终端中一切正常吗?
def suggest(product_idea):
return product_idea + "inator"
try:
product_idea = input("What is your product idea? ")
if len(product_idea) <= 3: #to catch the exception of people trying to use LESS THAN 3 characters
raise ValueError("Your product Idea is too short. Please re-submit with more than 3 characters.") #raise is used here to capture the error
except ValueError as err: #this is to handle the error from line 8
print("Oh no! That's not a valid value. Try again...")
print("({})".format(err)) #this is to handle the error from line 8
else:
print("Great idea!")
这是我从 MOOC(树屋)得到的错误:
Bummer: Oh no, there were 2 problems with your code, check the preview pane for more information
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
EE
======================================================================
ERROR: test_exception_not_raised (__main__.TestRaiseExecution)
----------------------------------------------------------------------
Traceback (most recent call last):
File "", line 23, in test_exception_not_raised
File "/workdir/utils/challenge.py", line 20, in execute_source
exec(src)
File "", line 5, in
EOFError: EOF when reading a line
======================================================================
ERROR: test_exception_raised (__main__.TestRaiseExecution)
----------------------------------------------------------------------
Traceback (most recent call last):
File "", line 30, in test_exception_raised
File "/workdir/utils/challenge.py", line 20, in execute_source
exec(src)
File "", line 5, in
EOFError: EOF when reading a line
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (errors=2)
解决方案
您似乎在错误的代码块下插入了“if else”语句的“else”部分。它属于 Try 块中的“Try”下。
def suggest(product_idea):
return product_idea + "inator"
try:
product_idea = input("What is your product idea? ")
if len(product_idea) <= 3: #to catch the exception of people trying to use LESS THAN 3 characters
raise ValueError("Your product Idea is too short. Please re-submit with more than 3 characters.") #raise is used here to capture the error
else:
print("Great idea!")
except ValueError as err: #this is to handle the error from line 8
print("Oh no! That's not a valid value. Try again...")
print("({})".format(err)) #this is to handle the error from line 8
此外,我创建了另一个版本的代码,您创建的函数 suggest() 在 Try 块中被调用。
def suggest(product_idea):
print(product_idea + "inator")
try:
product_idea = input("What is your product idea? ")
if len(product_idea) <= 3: #to catch the exception of people trying to use LESS THAN 3 characters
raise ValueError("Your product Idea is too short. Please re-submit with more than 3 characters.") #raise is used here to capture the error
else:
suggest(product_idea)
print("Great idea!")
except ValueError as err: #this is to handle the error from line 8
print("Oh no! That's not a valid value. Try again...")
print("({})".format(err)) #this is to handle the error from line 8
希望这可以帮助!
推荐阅读
- php - Access-Control-Allow-Origin:控制台出错,但文件仍在上传
- email - 删除 mailroot/Badmail 文件夹中的文件
- javascript - 动画存在在下一个 js 中仅适用于退出动画吗?
- apache - Apache 2.4 LDAP 身份验证第一次失败,第二次成功但仍然返回 HTTP 500
- javascript - API调用期间如何在reducer函数中返回状态
- leaflet - OSM 中缺少磁贴
- javascript - 定义一个使用参数创建并具有属性和方法的 JavaScript 对象
- postman - 通过 POST 人调用 Salesforce API
- python - 潜在语义分析后标记树状图簇
- pine-script - Q - Pinescript Sweep Indicator - 如何仅在被触摸时才显示线条?