有人能解释一下这个cod中实际参数和形式参数背后的原因吗

2024-10-01 17:37:57 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我遵循了其他人所做的一些指南/课程,并在正式参数和实际参数的讨论中遇到了这些指南/课程

当查看下面的代码时,我看到函数定义中有一些参数,例如作为f_amount传入变量(对吗?),但在函数本身中它不使用f_amount,而只使用amount

其次,当调用函数时,设置了两个变量并准备接收函数返回值,但我的问题是,当我可以从函数定义和调用中删除参数时,它为什么要传入这两个变量,而它仍然会执行相同的输出

def getBankDetails(f_amount, f_rate):
    print("Please enter the amount you have in the bank: ")
    amount = float(input())
    print("Please enter interest rate: ")
    rate = float(input())
    print(amount, rate)
    return amount, rate

def calculateInterest(f_amount, f_rate, f_interest):
    interest = amount / rate
    return interest

def displayInterest(f_interest):
    print("You have earned £", interest, "in interest this year")

amount = 0.0
rate = 0.0
interest = 0.0

amount, rate = getBankDetails(amount, rate)
interest = calculateInterest(amount, rate, interest)
displayInterest(interest)

vs

def getBankDetails():
    print("Please enter the amount you have in the bank: ")
    amount = float(input())
    print("Please enter interest rate: ")
    rate = float(input())
    print(amount, rate)
    return amount, rate

def calculateInterest():
    interest = amount / rate
    return interest

def displayInterest():
    print("You have earned £", interest, "in interest this year")

amount, rate = getBankDetails()
interest = calculateInterest()
displayInterest()

Tags: the函数ininput参数ratedefhave

热门问题