Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python For循环仅增加两个变量中的一个_Python_Python 3.x - Fatal编程技术网

Python For循环仅增加两个变量中的一个

Python For循环仅增加两个变量中的一个,python,python-3.x,Python,Python 3.x,我希望结果显示每年加利息后的总金额,但它只会增加一年,而不会增加金额。为什么? while True: try: investment = float(input('How much to invest : ')) interest = float(input('Interest rate : ')) break except ValueError: "Please enter a valid number" fo

我希望结果显示每年加利息后的总金额,但它只会增加一年,而不会增加金额。为什么?

while True:
    try:
        investment = float(input('How much to invest : '))
        interest = float(input('Interest rate : '))
        break
    except ValueError:
        "Please enter a valid number"

for year in range(10):
    money = investment + (investment * interest)
    print("Total money in year {} : {}".format((year+1), money))

逻辑错误。您的
投资
变量没有在循环中的每一轮中分配。

听起来您需要累积利息:

for year in range(10):
    investment += (investment * interest)
    print("Total money in year {} : {}".format((year + 1), investment))

money
在循环的每次迭代中都会重新计算,但不会更改,因为
investment
interest
一旦定义就不会更改
money=investment+(investment*interest)
之后添加一行设置
investment=money