Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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 如何向循环中的变量重复添加特定值?_Python - Fatal编程技术网

Python 如何向循环中的变量重复添加特定值?

Python 如何向循环中的变量重复添加特定值?,python,Python,我正试图编写一个程序,在这个程序中,我向变量weight添加0.165。我试着重复15次。但是,重量必须不断增加到0.165 15倍,例如13.365、13.53、13.495等 我将如何做到这一点?很抱歉,我对整个python编码还不熟悉,所以请指出我的代码中任何多余的错误 weight=int(input("Enter your weight")) newweight=weight+1 othernewweight=newweight*0.165 count=['1'

我正试图编写一个程序,在这个程序中,我向变量
weight
添加0.165。我试着重复15次。但是,
重量
必须不断增加到0.165 15倍,例如13.365、13.53、13.495等

我将如何做到这一点?很抱歉,我对整个python编码还不熟悉,所以请指出我的代码中任何多余的错误

   weight=int(input("Enter your weight"))
   newweight=weight+1
   othernewweight=newweight*0.165
   count=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
   for x in range(0,15):
       print("year", count+0", "othernewweight+0.65")

这将要求您在
float
类型中输入
weight
,然后对
weight
重复添加0.165次,共15次

weight=float(input("Enter your weight"))

for x in range(15):
    weight += 0.165
    print (round(weight,3)) #to print 3 decimals
输出:

Enter your weight 13.2                                                                                                                                                              
13.365                                                                                                                                                                             
13.53                                                                                                                                                                              
13.695                                                                                                                                                                             
13.86                                                                                                                                                                              
14.025                                                                                                                                                                             
14.19                                                                                                                                                                              
14.355                                                                                                                                                                             
14.52                                                                                                                                                                              
14.685
14.85                                                                                                                                                                              
15.015                                                                                                                                                                             
15.18                                                                                                                                                                              
15.345                                                                                                                                                                             
15.51                                                                                                                                                                              
15.675

这里有一些问题。首先,您将
count+0
othernewweight+0.65
放在引号内,因此它们被打印为文本,字面上是“othernewweight+0.65”,而不是您要查找的值。您还需要确保您实际上正在更新变量
weight+0.165
没有任何作用,除非您将其存储在某个位置,简称为
weight=weight+0.165
weight+=0.165

weight=int(input("Enter your weight"))
for x in range(15):
    weight += 0.165
    print(weight)

weight+=0.165
作为循环的第一行?不会改变你的行为,因为你从来不会在第二行之后使用
weight
,但这会满足你的要求。我不知道你的实际目标是什么。好的,谢谢你澄清我的错误。但是,是否可以将输入的变量乘以任何值,然后再加上0.165 15次?对不起,我如何加上例如“第一年,13.365,然后第二年,13.53”?请原谅我的无知。是的,在范围(15)中x的
行之前,只相乘一次,将
mulitplier=2
放入
weight*=multiplier
,其中2是要相乘的数字(或变量名)。要打印字符串和变量的组合,请使用python fstring(python 3.6及更高版本)<代码>打印(f“Year{x+1},{weight}”)
就可以了。希望这对戴尔有帮助。谢谢你的帮助,但是,在每种代码中都可以使用打印(圆形(重量,3))吗?@Callmedell当然可以。但是,
print()
语法是针对python3的。如果您使用python2,请使用
打印圆形(重量3)