What is the difference between a variable declaration outside the python novice_for statement and a variable declaration in the for statement?

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

Issue

When the number of friends is entered and the status of a 10×10 movie theater is entered, if Chul-soo and his friends can watch the movie in the same line, write a program that outputs what line it is, or –1.

n=int(input())
lst=[list(map(int,input().split())) for _ in range(10)]
temp=-1
cnt=0
for i in range(0,10):
  for j in range(0,10):
    if lst[i][j]==0:
      cnt+=1
    else:
      cnt=0
    if cnt>=n:
      temp=i+1

print(temp)

The code above shows 70 points

n=int(input())
lst=[list(map(int,input().split())) for _ in range(10)]
temp=-1

for i in range(0,10):
  cnt=0
  for j in range(0,10):
    if lst[i][j]==0:
      cnt+=1
    else:
      cnt=0
    if cnt>=n:
      temp=i+1

print(temp)

The code above will give you 100 points

The first code is to declare the cnt variable outside the for statement The second code is to declare the cnt variable in the for statement, but the score is different What's the difference? Please explain

python

2022-09-20 08:55

1 Answers

For the second code, if cnt=0 is in the for statement, it will initialize to zero every time the for statement turns. This means that #10 cnt = 0 is executed.

The first code is cnt=0 outside the for statement, so cnt=0 will be executed only once, right?

It would be helpful if you could write down how the cnt changes and calculate it yourself.


2022-09-20 08:55

If you have any answers or tips


© 2022 pinfo. All rights reserved.