[英]How to use input() in function()?
当我使用 Compound_interest(P, r, n, t) 调用该函数时,它显示错误名称 P 未定义,为什么?
def compound_interest(P, r, n, t):
P=int(input())
r=int(input())
n=int(input())
t=int(input())
A=P*(1+r/n)**(n*t)
return(f'Total Amount = {A}')
compound_interest(P, r, n, t)
最好是第一:因为一个方法应该只计算事物,从参数而不是询问新值
将值作为参数传递,您可以从主程序中的另一个输入源获得
def compound_interest(P, r, n, t):
A = P * (1 + r / n) ** (n * t)
return f'Total Amount = {A}'
# call compound_interest(1, 2, 3, 4)
或者您从函数内部的input()
获取值,因此您不需要将它们作为参数传递
def compound_interest():
P = int(input("P: "))
r = int(input("r: "))
n = int(input("n: "))
t = int(input("t: "))
A = P * (1 + r / n) ** (n * t)
return f'Total Amount = {A}'
# call compound_interest()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.