我对我的编程类进行了关于使用函数计算保险的评估。这是我一直想把工作代码,但不幸的是我失败:在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
您必须首先告诉我们_how_它的目标是工作和_how_是否真正起作用,包括所有适当的错误消息(如果有的话)。 – DyZ
寻求调试帮助的问题(“为什么这个代码不工作?”)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者无益。 –