对第二个功能感到困惑

Posted

技术标签:

【中文标题】对第二个功能感到困惑【英文标题】:Confused about the second Function 【发布时间】:2022-01-04 01:43:54 【问题描述】:

我对我的代码感到困惑,我认为我做的一切都是正确的,但我不确定如何让我的代码的第二部分工作,即“total_costs”函数。我现在看了很多视频,但似乎找不到关于这个的,有人可以向我解释为什么我的代码的第二部分不能工作吗?

def get_input():
    e1 = float(input("What is the monthly expense for House Payment. "))
    e2 = float(input("What is the monthly expense for Homeowners Insurance. "))
    e3 = float(input("What is the monthly expense for Car Payments. "))
    e4 = float(input("What is the monthly expense for Car Insurance. "))
    e5 = float(input("What is the monthly expense for Utilities. "))
    print("The monthly expenses total to",format(monthly, ',.2f'))
    print("The yearly expenses total to",format(yearly, ',.2f'))
    

def total_costs():
    monthly = e1 + e2 + e3 + e4 + e5
    yearly = monthly * 12
    return monthly, yearly
    
    

get_input()

【问题讨论】:

【参考方案1】:

您使用 e1,e2,e3,e4,e5 作为函数变量,因此范围仅限于 get_input 函数,您不能在 get_input 函数之外使用这些变量,除非您全局指定它们。

而且你没有调用total_cost函数。

问题是您在本地范围内使用变量而不是调用函数。

【讨论】:

【参考方案2】:

您的代码存在一些问题。首先,你没有调用total_costs(),所以它永远不会被执行。你也没有捕捉到结果。

让我们重新安排一下:

def get_input():
    e1 = float(input("What is the monthly expense for House Payment. "))
    e2 = float(input("What is the monthly expense for Homeowners Insurance. "))
    e3 = float(input("What is the monthly expense for Car Payments. "))
    e4 = float(input("What is the monthly expense for Car Insurance. "))
    e5 = float(input("What is the monthly expense for Utilities. "))
    monthly, yearly = total_costs()
    print("The monthly expenses total to",format(monthly, ',.2f'))
    print("The yearly expenses total to",format(yearly, ',.2f'))

def total_costs():
    monthly = e1 + e2 + e3 + e4 + e5
    yearly = monthly * 12
    return monthly, yearly
    
get_input() 

更好,但是当你尝试时,它会抱怨不知道e1。这是因为虽然看起来是这样,但 total_costs 并不知道 e1 和其他变量。您需要显式传递它。

#!/usr/bin/env python3
def get_input():
    e1 = float(input("What is the monthly expense for House Payment. "))
    e2 = float(input("What is the monthly expense for Homeowners Insurance. "))
    e3 = float(input("What is the monthly expense for Car Payments. "))
    e4 = float(input("What is the monthly expense for Car Insurance. "))
    e5 = float(input("What is the monthly expense for Utilities. "))
    monthly, yearly = total_costs(e1, e2, e3, e4, e5)
    print("The monthly expenses total to",format(monthly, ',.2f'))
    print("The yearly expenses total to",format(yearly, ',.2f'))

def total_costs(e1, e2, e3, e4, e5):
    monthly = e1 + e2 + e3 + e4 + e5
    yearly = monthly * 12
    return monthly, yearly
    
get_input() 

现在一切正常,正如您所期望的那样。现在你不一定会像这样构造你的代码。例如,您在get_input 中所做的事情并不需要它自己的函数,而是可能拥有像e1 这样的所有这些单个变量,您可以拥有一个像列表或字典这样的结构,您可以传递给total_costs,不过你很快就会知道的。

【讨论】:

【参考方案3】:

total_costs 无处被调用。

【讨论】:

什么意思?我该怎么称呼它?抱歉,我对编码还是很陌生 好的,所以 get_input() 将起作用,因为您已经调用了 get_input 函数。我在您的代码中看不到 total_costs 由 total_costs() 调用的任何地方 @LastCheck 我在你的代码中统计了 15 个函数调用,所以你知道怎么做。 我不完全确定某些事情的术语,我不知道我输入的内容的底部被认为是一个电话 如果你想在 get_input() 之后在 total_costs 中使用每月和每年,那么 get_input 必须在函数 get_input() 结束时“每月、每年返回”。【参考方案4】:

首先,total_costs() 没有被调用。如果它没有被调用,你怎么能期望它被执行?

其次,甚至如果你打电话给total_costs,它仍然会给出NameError: name 'e1' is not defined。这是因为get_input()中的变量是局部变量,所以在get_input()函数结束时会被删除。 因此,您需要从get_input() 返回变量,或者使用global 关键字使它们不限于get_input(),尽管我强烈建议您使用第一种方法,因为global 使调试成为一场噩梦。

代码:

def get_input():
    e1 = float(input("What is the monthly expense for House Payment. "))
    e2 = float(input("What is the monthly expense for Homeowners Insurance. "))
    e3 = float(input("What is the monthly expense for Car Payments. "))
    e4 = float(input("What is the monthly expense for Car Insurance. "))
    e5 = float(input("What is the monthly expense for Utilities. "))
    print("The monthly expenses total to",format(monthly, ',.2f'))
    print("The yearly expenses total to",format(yearly, ',.2f'))
    return e1,e2,e3,e4,e5

def total_costs():
    monthly = e1 + e2 + e3 + e4 + e5
    yearly = monthly * 12
    return monthly, yearly

get_input()
total_costs()

def get_input():
    global e1,e2,e3,e4,e5
    e1 = float(input("What is the monthly expense for House Payment. "))
    e2 = float(input("What is the monthly expense for Homeowners Insurance. "))
    e3 = float(input("What is the monthly expense for Car Payments. "))
    e4 = float(input("What is the monthly expense for Car Insurance. "))
    e5 = float(input("What is the monthly expense for Utilities. "))
    print("The monthly expenses total to",format(monthly, ',.2f'))
    print("The yearly expenses total to",format(yearly, ',.2f'))

def total_costs():
    monthly = e1 + e2 + e3 + e4 + e5
    yearly = monthly * 12
    return monthly, yearly

get_input()
total_costs()

【讨论】:

以上是关于对第二个功能感到困惑的主要内容,如果未能解决你的问题,请参考以下文章

Java逻辑运算符

对函数调用中的单指针和双指针参数感到困惑

对减少和箭头功能感到困惑[重复]

带有动态功能指南的即时动态功能

对通过多个功能转发引用感到困惑

对 Haskell 类型推断感到困惑