首页 > 解决方案 > Python输入函数复制

问题描述

为什么我的输入被问了两次是有原因的吗?我需要使用三个单独的函数来使用用户输入来计算公式。

def values():
    initInv=input("Enter initial investment ")
    rate=input("Enter the interest rate in the form of a decimal ")
    freq=input("Enter the frequency that the interest is paid out per year ")
    years=input("Enter the number of years the interest is compounded for ")
    return initInv, rate, freq, years

w, x, y, z=values()

def typecast():
    invest=int(w)
    RATE=float(x)
    FREQ=int(y)
    time=int(z)
    return invest, RATE, FREQ, time

first, second, third, fourth=typecast()

def formula():
    P=first
    r=second
    n=third
    t=fourth
    A=(P*(1+(r/n))**(n*t))
    return A

values()
typecast()
formula()

标签: pythoninput

解决方案


你打values了两次电话。定义后w, x, y, z=values()一次:然后在最后:values()

编辑:与typecast. 你也叫它两次。

这是删除重复项的代码。

def values():
    initInv=input("Enter initial investment ")
    rate=input("Enter the interest rate in the form of a decimal ")
    freq=input("Enter the frequency that the interest is paid out per year ")
    years=input("Enter the number of years the interest is compounded for ")
    return initInv, rate, freq, years

def typecast():
    invest=int(w)
    RATE=float(x)
    FREQ=int(y)
    time=int(z)
    return invest, RATE, FREQ, time

def formula():
    P=first
    r=second
    n=third
    t=fourth
    A=(P*(1+(r/n))**(n*t))
    return A

w, x, y, z=values()
first, second, third, fourth=typecast()
A = formula()

推荐阅读