Python 3.x 我试图在Python中切换参数,而不使用全局变量
如何将这些变量返回到主函数中?(这是类的一部分,我不能使用全局。我还必须将主函数的打印部分分开。未分配AssessedTotalValue和PropertyTaxTotal等错误。将参数变量放入函数中很有效,但为什么它们不能从中出来Python 3.x 我试图在Python中切换参数,而不使用全局变量,python-3.x,Python 3.x,如何将这些变量返回到主函数中?(这是类的一部分,我不能使用全局。我还必须将主函数的打印部分分开。未分配AssessedTotalValue和PropertyTaxTotal等错误。将参数变量放入函数中很有效,但为什么它们不能从中出来 def PropertyTax(): global PropertyTaxPercent PropertyTaxPercent = .64 PropertyValue = int(input( "Enter Property Value: "
def PropertyTax():
global PropertyTaxPercent
PropertyTaxPercent = .64
PropertyValue = int(input( "Enter Property Value: "))
AssessedValue(PropertyValue)
PropertyTaxValue(AssessedTotalValue)
print ("The Property Assessed Value is: ", AssessedTotalValue)
print ("The Property Tax is: ", PropertyTaxTotal)
def AssessedValue(PropertyValue):
global AssessedPercentValue
AssessedPercentValue = 0.60
AssessedTotalValue = PropertyValue * AssessedPercentValue
def PropertyTaxValue(AssessedTotalValue):
PropertyTaxValue = AssessedTotalValue / 100
PropertyTaxTotal = PropertyTaxValue * PropertyTaxPercent
PropertyTax()
您不需要将变量定义为全局变量。 可以在函数中使用return语句,使函数将值返回到调用函数的位置 例如 总和=加(1,6) 打印(总和) 将输出7
鉴于此,您可以将您的计划修改为以下内容:
def PropertyTax():
PropertyTaxPercent = .64
PropertyValue = int(input( "Enter Property Value: "))
AssessedTotalValue = AssessedValue(PropertyValue)
PropertyTaxTotal = PropertyTaxValue(AssessedTotalValue,PropertyTaxPercent)
print ("The Property Assessed Value is: ", AssessedTotalValue)
print ("The Property Tax is: ", PropertyTaxTotal)
def AssessedValue(PropertyValue):
AssessedPercentValue = 0.60
AssessedTotalValue = PropertyValue * AssessedPercentValue
return AssessedTotalValue
def PropertyTaxValue(AssessedTotalValue,PropertyTaxPercent):
PropertyTaxValue = AssessedTotalValue / 100
PropertyTaxTotal = PropertyTaxValue * PropertyTaxPercent
return PropertyTaxTotal
PropertyTax()
您已经了解了参数。现在查找“返回值”。
def PropertyTax():
PropertyTaxPercent = .64
PropertyValue = int(input( "Enter Property Value: "))
AssessedTotalValue = AssessedValue(PropertyValue)
PropertyTaxTotal = PropertyTaxValue(AssessedTotalValue,PropertyTaxPercent)
print ("The Property Assessed Value is: ", AssessedTotalValue)
print ("The Property Tax is: ", PropertyTaxTotal)
def AssessedValue(PropertyValue):
AssessedPercentValue = 0.60
AssessedTotalValue = PropertyValue * AssessedPercentValue
return AssessedTotalValue
def PropertyTaxValue(AssessedTotalValue,PropertyTaxPercent):
PropertyTaxValue = AssessedTotalValue / 100
PropertyTaxTotal = PropertyTaxValue * PropertyTaxPercent
return PropertyTaxTotal
PropertyTax()