O
O
onekrugoikov2020-05-18 19:46:26
Python
onekrugoikov, 2020-05-18 19:46:26

How to repeat the cycle?

And so, look, I didn’t ask a lot of the wrong question.

There is a code

a = 0
def new():
    for line in range(2):
        print(a)
        a += 1
new()


If you execute this code, then the number 0 will be displayed and the loop will end. The question is, how can I continue it further?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmshar, 2020-05-18
@onekrugoikov

Yes, it is possible in different ways.
You can set a global variable.

a = 0
def new():
    global a
    for line in range(2):
        print(a)
        a +=1

new()

0
1

You can pass it as a parameter, like so:
a = 0
def new(a):
    for line in range(2):
        print(a)
        a +=1
new(a)

0
1

In general, this is described in any Python tutorials. Have you tried reading?

D
DollyPapper, 2020-05-18
@DollyPapper

Your code won't run at all. Variable anot declaredglobal

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question