function - 如果失败,我如何从python中的函数返回值

标签 function python-2.7 error-handling

我想在用户输入某些值时进行检查。例如,如果用户输入的值超出250-10000的范围,则它应显示一条消息,警告他们输入了错误的值并出租给该值。如果输入正确的值,我的代码将起作用。如果输入的值不正确,则会收到一条消息,但现在如果输入的值正确,我该如何在main()中将其返回给fc。我愿意接受任何积极的反馈!谢谢您的帮助!

import ctypes


def build_fc():
    print "Please enter fc between 250-10000:"
    fc_string_input = raw_input()
    fc_float = float(fc_string_input)
    if (fc_float)>=250 and (fc_float<=10000):
         return float(fc_string_input)
    else:
         setup_fc_error_message()
         build_fc()

def setup_fc_error_message():
    lines = ['fc is out of range: Please Enter a value from 250-10000:']
    MessageBox = ctypes.windll.user32.MessageBoxA
    MessageBox(None, "\n".join(lines), 'Setup Info', 0) 

def main():

    fc = build_fc()
    print fc

if __name__ == "__main__":
    main()

最佳答案

代替,

else:
   setup_fc_error_message()
   build_fc()

尝试:
else:
   setup_fc_error_message()
   return build_fc()

关于function - 如果失败,我如何从python中的函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31173588/

相关文章:

error-handling - 为什么用errexit不能与命令列表一起使用?

php - 未定义索引 (Laravel)

循环遍历大型 numpy 整数数组时 Python 内存溢出,一次性转换

python - 为什么我会收到 TypeError : 'module' object is not callable in python?

ios - 在 Xcode 7 中从 Unity 构建 iOS 时出错

c - C 中的无效输入

javascript - 在这种情况下 'pass result to parameter' 意味着什么?

c - 将一个字符串复制到另一个字符串的程序(包含该函数)正在打印奇怪的字符

Python:从函数中减去一个 float (为了调用 scipy.newton())

python - 函数迭代打印但不返回