简体   繁体   中英

How can I make this loop increase by 1.8 each iteration?

print("Year\tRise (in mm)")
print("------------------")

for number in range(1, 26):
    rise = number + 1.8
    print(number, '\t', rise)

So basically, rise is supposed to increase by 1.8 each time.

1 = 1.8 2 = 3.6 so on

Make rise adding instead of number :

rise = 0
for number in range(1, 26):
    rise += 1.8
    print(number, '\t', rise)

不知道 Python 的语法,但它应该是乘法来得到你想要的,而不是加法,所以它应该是:rise = number * 1.8

rise = 0.0

print("Year\tRise (in mm)")
print("------------------")

for number in range(1, 26):
    rise += 1.8
    print(number, '\t', format(rise, '.2f'))

i figured it out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM