在Python中使用带变量的函数

时间:2017-09-08 07:23:49

标签: python algorithm python-3.x function

我对我的编程课程有一个关于使用函数计算保险的评估。这是我试图开始工作的代码,但不幸的是我失败了:

import time

def main():  
        print('Welcome to "Insurance Calculator" ')
        type_I, type_II, type_III = inputs()
        calculationAndDisplay()
        validation()
        time.sleep(3)       

def inputs():
    try:
        type_I = int(input("How many  Type I policies were sold? "))
        type_II = int(input("How many  Type II policies were sold? "))
        type_III = int(input("How many  Type III policies were sold? ")) 
        return type_I, type_II, type_III
    except ValueError:
        print("Inputs must be an integer, please start again")
        inputs()


def calculationAndDisplay():
    type_I *= (500/1.1)
    type_II *= (650/1.1)
    type_III *= (800/1.1)
    print("The amount of annual earned for type_I is: $", type_I)
    print("The amount of annual earned for type_I is: $", type_II)
    print("The amount of annual earned for type_I is: $", type_III)

def validation():
    cont = input("Do you wish to repeat for another year? [Y/N]: ")
    if cont == 'Y' or cont == 'y':
        main()
    elif cont == 'N' or cont == 'n':
        print('Thank You! ------ See You Again!')    
    else:
        print("I'm sorry, I couldn't understand your command.")
        validation()

main()

我最终通过将所有输入,计算和显示塞入一个功能来实现它。我只是想知道我怎么能按照预期的方式工作......

编辑:该程序旨在让用户输入已售出的政策数量并显示税前总额。当我输入几个数字输入时,它会给我以下错误

Welcome to "Insurance Calculator" 
How many  Type I policies were sold? 3
How many  Type II policies were sold? 3
How many  Type III policies were sold? 3
Traceback (most recent call last):
  File "C:\Users\crazy\Desktop\assessment 2.py", line 38, in <module>
    main()
  File "C:\Users\crazy\Desktop\assessment 2.py", line 5, in main
    type_I, type_II, type_III = inputs()
TypeError: 'NoneType' object is not iterable

编辑:我将返回行移动到建议的行,现在它给了我一个未绑定的变量错误:

Welcome to "Insurance Calculator" 
How many  Type I policies were sold? 5
How many  Type II policies were sold? 5
How many  Type III policies were sold? 5
Traceback (most recent call last):
  File "C:\Users\crazy\Desktop\assessment 2.py", line 39, in <module>
    main()
  File "C:\Users\crazy\Desktop\assessment 2.py", line 6, in main
    calculationAndDisplay()
  File "C:\Users\crazy\Desktop\assessment 2.py", line 22, in 
calculationAndDisplay
    type_I *= (500/1.1)
UnboundLocalError: local variable 'type_I' referenced before 
assignment

2 个答案:

答案 0 :(得分:1)

问题是,您将值保存在仅存在于main方法内但不存在于全局范围内的变量中:

type_I, type_II, type_III = inputs()
calculationAndDisplay()

执行此操作时,calculationAndDisplay()方法不知道值。您可以通过向此函数添加参数来解决此问题,如下所示:

def calculationAndDisplay(type_I, type_II, type_III):
    #your code

编辑:当您在同一方法中执行所有计算时,您的代码工作没有任何问题,因为现在所有变量都在同一范围内创建。如果使用方法,则必须使用函数参数/参数(更好的解决方案)或使用global变量(错误的解决方案,因为它破坏了函数的概念)。

在这种情况下,您稍后再次调用type_I后想要使用calculationAndDisplay()等的修改值,您必须在此函数中返回修改后的值,以便您及以上使用此代码:

def calculationAndDisplay(type_I, type_II, type_III):
    #your code

    return type_I, type_II, type_III

答案 1 :(得分:0)

您提到的错误是由于inputs()中的return语句缩进造成的。返回语句应该在输入函数内(因为在你的情况下,input()不返回type_I,type_II,type_II)。参数也应该传递给calculateAndDisplay(type_I,type_II,type_III)。