I change the Python while statement to for statement, but I don't get the same execution value

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

###Global variable declaration part##
i,k=0,0

##Main code part ##

i=0
while i<9:
    if i<5:
        k=0
        while k<4-i:
            print('  ', end='')
            k+=1
        k=0
        while k<i*2+1:
            print('\u2665', end='')

###Global variable declaration part##
i,k=0,0

##Main code part ##

for i in range(0,9):
    if i<5:
        for k in range(0,4-i):
            print('  ',end='')
            k+=1
        for k in range(0,i*2+1):
            print('\u2665',end='')

I changed it to this for part Poor understanding of for does not result in the same execution value
I ask you what you overlooked

while-loop for python

2022-09-20 10:39

1 Answers

Hello.

In while, k+=1 appears to have been used to increase or decrease repeatedly in the k iteration.

However, in for-range, range itself functions as an increase or decrease, so I don't think the corresponding part needs to be written again.

The Hashcode self-compiler has an error in the print section, but it seems to be a problem.


2022-09-20 10:39

If you have any answers or tips


© 2022 pinfo. All rights reserved.