gpt4 book ai didi

python - 我如何复制此 try/except 代码中的所有其他输入?

转载 作者:行者123 更新时间:2023-12-01 23:38:48 26 4
gpt4 key购买 nike

我如何在所有其他输入中复制相同的尝试/除了你看到的第一个输入?

import math
import sys
import time

while True:
try:
Money = float(input("How much are you depositing?"))
except:
print("please enter numbers")
continue
AIR = float(input("What is the annual interest rate?"))
#Annual Interest Rate
n = int(input("How many times a year do you want the interst compounded for?"))
# number of times the interst compounds
t = int(input("How many years are you investing/borrowing the money?"))
# total number of years the money is being invested/borrowed for
CCI = Money *(1+AIR/n)**(n*t)
# Calculated Compound Interest
print(math.trunc(CCI));
# main program

最佳答案

将该逻辑提取到一个您可以重用的函数中:

def read(type_, prompt):  # using "type_" not to shadow built-in "type"
while True:
try:
return type_(input(prompt))
except (TypeError, ValueError):
print("Please enter a {}".format(type_.__name__))

Money = read(float, "How much are you depositing?\n")
AIR = read(float, "What is the annual interest rate?\n")
n = read(int, "How many times a year do you want the interst compounded for?\n")
t = read(int, "How many years are you investing/borrowing the money?\n")

CCI = Money *(1+AIR/n)**(n*t)
print(math.trunc(CCI))

关于python - 我如何复制此 try/except 代码中的所有其他输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65199096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com