每當我運行此操作時,它只會繼續獲取p
值,而不是計算出的賬戶未來值?!當用戶輸入現值,利率和年數時,計算賬戶的未來價值
def main():
p=eval(input("Enter in the present value of the account: "))
i=eval(input("Enter in the monthly interest rate(%): "))
I=eval(str(i//100))
t=eval(input("Enter the number of months that that the money will be in the account: "))
print(futureValue(p, I, t),"Is the future value of your account!")
def futureValue(p, I, t):
return p*((1 + I) ** t)
main()
對於所有這一切的愛是聖潔的,___不是'eval'原始用戶input___。即使我們忽略了整個「安全問題的定義」,也意味着用戶的小錯別字可以完成意想不到的事情,而且無法開始預測或處理。如果目標是轉換爲「int」或「float」或「decimal.Decimal」,則使用它們的構造函數。如果目標是接受'int'或'float'或任何其他Python文字,請使用['ast.literal_eval'](https://docs.python.org/3/library/ast.html#ast.literal_eval) ,它接受Python文字,但不是任意代碼。 – ShadowRanger