Python 3.x 如果函数错误或参数错误?

Python 3.x 如果函数错误或参数错误?,python-3.x,Python 3.x,我正在尝试获取要考虑的MinValue和MaxValue,并打印一条错误消息,然后继续循环 它应该用设置的参数检查输入,如果输入在参数的任一侧,那么它应该打印错误消息,然后继续循环!但它似乎跳过了If函数并打印“数字是‘x’” def inputInt(提示,errorMessage='Invalid input-重试',minValue=3,maxValue=None): 尽管如此: 值=输入('输入值:') 尝试: 返回int(值) 除值错误外: 打印(错误消息) 如果(minValue!=

我正在尝试获取要考虑的MinValue和MaxValue,并打印一条错误消息,然后继续循环

它应该用设置的参数检查输入,如果输入在参数的任一侧,那么它应该打印错误消息,然后继续循环!但它似乎跳过了If函数并打印“数字是‘x’”

def inputInt(提示,errorMessage='Invalid input-重试',minValue=3,maxValue=None):
尽管如此:
值=输入('输入值:')
尝试:
返回int(值)
除值错误外:
打印(错误消息)
如果(minValue!=“无”且值maxValue):
打印(errorMessage='值高于最大值')
value=inputInt('输入一个int:')
打印('值为',值)
def inputFloat(提示,errorMessage='无效输入-重试',minValue='None',maxValue='None'):
尽管如此:
值=输入('输入值:')
尝试:
返回浮动(值)
除值错误外:
打印(错误消息)
如果(minValue!='None'和int(value)
return
退出函数并“返回”到调用它的位置:

def f():
    return 1
    raise RuntimeError("Function continued after return?!?!")
print( f() ) #will not raise the error, prints 1
因此,要在转换后执行额外操作,需要将其存储为变量,然后在末尾返回:

while True:
    value = input(...) #you still probably want to use the prompt variable here
    try:
        value = float(value)
    except ValueError:
        print(errorMessage)
        continue #continue with loop, go back to top of loop

    #other code here

    return value

你明白
return
语句的作用吗?噢,嗨,库尔特。。。这是来自的代码,因此我已经知道您不理解
return
…我不确定您为什么在
打印中使用
=
(errorMessage='Value…')
我想您需要一个逗号,但不确定。。。还有
if(maxValue!=“None”…):
可能希望检查
None
而不是
“None”
while True:
    value = input(...) #you still probably want to use the prompt variable here
    try:
        value = float(value)
    except ValueError:
        print(errorMessage)
        continue #continue with loop, go back to top of loop

    #other code here

    return value