简体   繁体   English

如何在 function() 中使用 input()?

[英]How to use input() in function()?

When I call the function using compound_interest(P, r, n, t) it shows error name P is not defined, why ?当我使用 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)

Best is first : because a method should only compute things, from parameters and not asking new value最好是第一:因为一个方法应该只计算事物,从参数而不是询问新值

Either pass the values as parameters you'd get from another source of input in the main program将值作为参数传递,您可以从主程序中的另一个输入源获得

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)

Or you get the values from the input() inside the function so you don't need to passe them as parameters或者您从函数内部的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM