2017-09-08 61 views
-2

我对我的编程类进行了关于使用函数计算保险的评估。这是我一直想把工作代码,但不幸的是我失败:在Python中对变量使用函数

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 
+1

您必须首先告诉我们_how_它的目标是工作和_how_是否真正起作用,包括所有适当的错误消息(如果有的话)。 – DyZ

+1

寻求调试帮助的问题(“为什么这个代码不工作?”)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者无益。 –

回答

1

的问题是,你保存在其中仅main方法内而不是在全球范围内存在变量的值:

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

这样做,calculationAndDisplay()方法不知道这些值。你可以解决这个问题,通过添加参数,这个功能是这样的:

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

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

在这种情况下,你以后想使用的type_I等修改后的值再次调用calculationAndDisplay()后,你必须在这个函数返回修改后的值,让你和了此代码:

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

    return type_I, type_II, type_III 
+0

如果函数'calculateAndDisplay'仅被调用一次,因为参数的原始值未更新,则您的解决方案可以正常工作。 – DyZ

+0

@DYZ是的,但这是预期的情况,不是吗? – FlashTek

+0

我不知道。只有OP知道。 – DyZ

0

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