E
E
Eugene2020-03-02 00:11:11
Vue.js
Eugene, 2020-03-02 00:11:11

How to repeat the cycle if necessary?

There is a loop that must be repeated a certain number of times (variable much)
After its execution, it will ask whether it needs to be repeated the same number of times or not. If yes, the cycle repeats.
I tried like this, it didn’t work, it looks like the variable in the loop cannot be changed in the process

much = int(input('how much? '))


for i in range(much):
    print('Privet')
    if i == much-1:
        more = str(input('Еще? '))
        if more == 'yes':
            much = much + much
        else:
            break

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-07-15
@architawr

s-switch(@switch="switch")

Cool. Are you aware that switch is a keyword? Rename the handler. Well, or write this.switch.

S
Sergey Sokolov, 2020-03-02
@evgentor

bad answer
Поскольку кусок кода с циклом может выполняться несколько раз, хорошо бы его вытащить в отдельную функцию, к которой и обращаться по мере необходимости:
def loop(n):
    for i in range(n):
        print('Privet')

much = int(input('how much? '))
loop(much)

more = str(input('Еще? '))
if more == 'yes':
    loop(much)

Probably, I misunderstood the conditions .. There can be as many repetitions of “more” as you like, and not 1?
Then it is possible and directly in a cycle. But an iterator will not work here range(), but it is better to get by with a variable that is reduced from muchto 0:
much = int(input('how much? '))
i = much
while i > 0:
    print('Privet')
    i = i - 1
    if i == 0:
        more = str(input('Еще? '))
        if more == 'yes':
            i = much

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question