【问题标题】:Python unresolved referencePython未解决的参考
【发布时间】:2019-11-17 01:23:39
【问题描述】:

如果有人可以帮我找到错误。变量名称(monthly_budgets)在函数之外未解析,并且在调用时。我是 python 的新程序员,因此将不胜感激帮助和任何提示。

def monthlbudgetoverview(monthly_expense_budget, monthly_savings_budget, monthly_spending_budget):
    monthly_expense_budget = float(budget_income_five * 4)
    monthly_savings_buget = float(budget_income_seven * 4)
    monthly_spending_budget = float(budget_income_eight * 4)
    return monthly_expense_budget, monthly_savings_budget, monthly_spending_budget

print("Your monthly bill budget is ${}" .format(monthly_expense_budget))
print("Your monthly savings budget is ${}" .format(monthly_savings_buget))
print("Your monthly spending budget is ${}" .format(monthly_spending_budget))

monthlbudgetoverview(monthly_expense_budget, monthly_spending_budget, monthly_spending_budget)

【问题讨论】:

  • 您不使用 monthlbudgetoverview 的返回值...您调用它并在其中有一个 return 但您需要 1)在打印之前进行调用 2)分配给返回对某事有价值。
  • 尝试将print 语句放在monthlbudgetoverview 方法中,然后再返回。

标签: python python-3.x scope


【解决方案1】:

假设您的代码中的所有其他内容都可以正常工作,这是正确的形式。

def monthlbudgetoverview(monthly_expense_budget, monthly_savings_budget, monthly_spending_budget):
    monthly_expense_budget = float(budget_income_five * 4)
    monthly_savings_buget = float(budget_income_seven * 4)
    monthly_spending_budget = float(budget_income_eight * 4)
    return monthly_expense_budget, monthly_savings_budget, monthly_spending_budget


monthly_expense_budget, monthly_savings_budget, \
monthly_spending_budget = monthlbudgetoverview(monthly_expense_budget, 
                                               monthly_spending_budget, monthly_spending_budget)

print("Your monthly bill budget is ${}".format(monthly_expense_budget))
print("Your monthly savings budget is ${}".format(monthly_savings_buget))
print("Your monthly spending budget is ${}".format(monthly_spending_budget))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-03
    • 2019-04-05
    • 1970-01-01
    • 2015-09-04
    • 2014-05-05
    • 2021-08-17
    • 2016-12-14
    • 2020-05-27
    相关资源
    最近更新 更多