user14333268
user14333268

Reputation:

How to print the value of number during a "x" number of loops?

basically, Im trying to use a while loop to make an arithmetic sequence of numbers the code i made being rather simple:

x=2
while True:
    x=x+3
    print(x)

but a problem with this code of course is that there is no way i can find the value of x during a certain number of loops and rather this code prints every possible value in the sequence.

Does anyone know how i can make the code in a way that i can choose to print the value of x after a certain number of loops?

Upvotes: 0

Views: 93

Answers (1)

user2390182
user2390182

Reputation: 73470

The easiest for a known nuber of iterations is a for-loop:

x = 2
iterations = 5

for _ in range(iterations):
    x = x + 3

print(x)

This of course can be shortened in this particular example:

x = x + 3 * iterations

Upvotes: 1

Related Questions