用变量替换函数?
Replacing a Function with a Variable?
我觉得这个问题会很愚蠢,因为我不太确定如何表达它。我认为最好不要过度解释我的问题。
好的,Code Academy 他们给了这个作业:
在您现有的代码下方,定义一个名为 trip_cost
的函数,它接受两个参数,city
和 days
。
像上面的例子一样,让你的函数 return 调用 rental_car_cost(days)
、hotel_cost(days)
和 plane_ride_cost(city)
的 sum ] 函数。
以下为什么/如何工作(我明白了,但我不明白。):
It is completely valid to call the hotel_cost(nights)
function with
the variable days
. Just like the example above where we call
double(n)
with the variable a, we pass the value of days
to the
new function in the argument nights
.
我做对了代码并通过了:
def hotel_cost(nights):
return 140 * nights
def plane_ride_cost(city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
elif city == "Los Angeles":
return 475
def rental_car_cost(days):
cost = 40 * days
if days >= 7:
cost -= 50
elif days >= 3:
cost -= 20
return cost
def trip_cost(city, days):
return plane_ride_cost(city) + hotel_cost(days) + rental_car_cost(days)
一开始我放的是hotel_cost(nights)
,后来改成hotel_cost(days)
就通过了。现在它让我很困惑。该函数如何以及为什么从 def
中获取变量?我想我是在问它是如何工作的?
我正在尝试理解如何表达这个问题。有人可以打破这个吗?哈哈,我什至不知道取什么标题,因为这个概念有点混乱。我的意思是我明白了,但我不明白(如果这说得通的话。)这个 only 在定义函数时有效吗?
此外,为什么它被称为 "Calling a function",而在这种情况下它实际上是 替换 一个函数?
当您定义函数时,例如:def hotel_cost(nights)
变量 nights
只是您传递给函数的任何内容的名称。它不必与您发送的任何内容的名称相匹配。
所以当你说 def trip_cost(city, days):
Python 是在说:
“好的,我希望此函数中有两个变量,一个名为 city
,另一个名为 days
。
所以当你尝试调用
def trip_cost(city, days):
return plane_ride_cost(city) + hotel_cost(nights) + rental_car_cost(days)
Python 说,“我只知道 city
和 days
。对我来说没有 nights
变量这样的东西,你会得到一个错误。
这种情况甚至如果你调用类似cost = trip_cost(city, nights)
的东西。虽然它是 nights
给调用者。就函数所知,一旦它在函数内部执行,它只会将第二个 thing 视为 days
。
在更专业的定义中,这称为 scope。一个函数的范围是它在自身之外可以 "see" 和不可以的。在这种情况下,trip_cost
唯一能看到的是给它的两个东西,位置 1 中定义为 city
的东西和位置 2 中定义为 days
的东西.
我正在上 codecademy 课程,这个话题让我很困惑。但是我了解到 argument
实际上就像变量的定义,如果你这样做可以是 changed.so:
def add(x):
return x + 10
函数的作用是在函数内部,并且是缩进的。论证,就像一个数学variable
。它代表另一个值,或variable
。这样 argument
就可以被变量替换,每次 variable
在 function
中弹出时,它都会被替代变量替换。 arguments
可以有任意多个。 功能(明白了吗?)示例是:
def math(x, y, z): # x is the operation
if x == 'add':
return y + x
if x == 'subtract':
return y - x
if x == 'multiply':
return y * x
if x == 'divide':
return x / y
mat = input('Enter an operation: ')
ma = input('Enter the first number:')
nat = input('Enter the second number:')
print math(mat, ma, nat) # This prints the function and everything returned to it, with the new arguments
我觉得这个问题会很愚蠢,因为我不太确定如何表达它。我认为最好不要过度解释我的问题。
好的,Code Academy 他们给了这个作业:
在您现有的代码下方,定义一个名为 trip_cost
的函数,它接受两个参数,city
和 days
。
像上面的例子一样,让你的函数 return 调用 rental_car_cost(days)
、hotel_cost(days)
和 plane_ride_cost(city)
的 sum ] 函数。
以下为什么/如何工作(我明白了,但我不明白。):
It is completely valid to call the
hotel_cost(nights)
function with the variabledays
. Just like the example above where we calldouble(n)
with the variable a, we pass the value ofdays
to the new function in the argumentnights
.
我做对了代码并通过了:
def hotel_cost(nights):
return 140 * nights
def plane_ride_cost(city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
elif city == "Los Angeles":
return 475
def rental_car_cost(days):
cost = 40 * days
if days >= 7:
cost -= 50
elif days >= 3:
cost -= 20
return cost
def trip_cost(city, days):
return plane_ride_cost(city) + hotel_cost(days) + rental_car_cost(days)
一开始我放的是hotel_cost(nights)
,后来改成hotel_cost(days)
就通过了。现在它让我很困惑。该函数如何以及为什么从 def
中获取变量?我想我是在问它是如何工作的?
我正在尝试理解如何表达这个问题。有人可以打破这个吗?哈哈,我什至不知道取什么标题,因为这个概念有点混乱。我的意思是我明白了,但我不明白(如果这说得通的话。)这个 only 在定义函数时有效吗?
此外,为什么它被称为 "Calling a function",而在这种情况下它实际上是 替换 一个函数?
当您定义函数时,例如:def hotel_cost(nights)
变量 nights
只是您传递给函数的任何内容的名称。它不必与您发送的任何内容的名称相匹配。
所以当你说 def trip_cost(city, days):
Python 是在说:
“好的,我希望此函数中有两个变量,一个名为 city
,另一个名为 days
。
所以当你尝试调用
def trip_cost(city, days):
return plane_ride_cost(city) + hotel_cost(nights) + rental_car_cost(days)
Python 说,“我只知道 city
和 days
。对我来说没有 nights
变量这样的东西,你会得到一个错误。
这种情况甚至如果你调用类似cost = trip_cost(city, nights)
的东西。虽然它是 nights
给调用者。就函数所知,一旦它在函数内部执行,它只会将第二个 thing 视为 days
。
在更专业的定义中,这称为 scope。一个函数的范围是它在自身之外可以 "see" 和不可以的。在这种情况下,trip_cost
唯一能看到的是给它的两个东西,位置 1 中定义为 city
的东西和位置 2 中定义为 days
的东西.
我正在上 codecademy 课程,这个话题让我很困惑。但是我了解到 argument
实际上就像变量的定义,如果你这样做可以是 changed.so:
def add(x):
return x + 10
函数的作用是在函数内部,并且是缩进的。论证,就像一个数学variable
。它代表另一个值,或variable
。这样 argument
就可以被变量替换,每次 variable
在 function
中弹出时,它都会被替代变量替换。 arguments
可以有任意多个。 功能(明白了吗?)示例是:
def math(x, y, z): # x is the operation
if x == 'add':
return y + x
if x == 'subtract':
return y - x
if x == 'multiply':
return y * x
if x == 'divide':
return x / y
mat = input('Enter an operation: ')
ma = input('Enter the first number:')
nat = input('Enter the second number:')
print math(mat, ma, nat) # This prints the function and everything returned to it, with the new arguments