Python for Moon Question

Asked 2 years ago, Updated 2 years ago, 114 views

Number 1

hap=0
for a in range(1,11):
    hap=hap+a
    print(hap)

Value is

1
3
6
10
15
21
28
36
45
55

Number 2

hap=0
for a in range(1,11):
 hap=hap+a
print(hap)

The price 55

Number 3

hap=0
for a in range(1,11):
hap=hap+a
print(hap)

Value: error

I don't know the difference between 1, 2, and 3

And

for a in range(0,3)
    print('bc')

Value is

bc
bc
bc

That's right

In other books, I saw that you pay by matching a (variable) and bc... Is it okay not to match bc with a (variable)? I still don't understand the for statement

python for

2022-09-22 21:50

2 Answers

In Python, block syntax is done through indentation.

for pattern in patterns:
    print(pattern)

It's all indented in number one. That's why the print including hap=hap+a is repeatedly taken

In number 2, only the hap= hap+a part is indented. So there's only hap= hap+a in the for door block. That's why there's a print at the end.

Indent failed in number 3. This results in an error. Take a look at the lecture below~

http://tryhelloworld.co.kr/courses/ Python-introduction/lessons/for-in-list


2022-09-22 21:50

Indent is important for Python

for i in list:
    print(i)

I say it in another languageCotton

for (i in list) { 
    System.out.println(i);
}

That's what happens.

Indent to separate code blocks. So if the indentation doesn't fit, there will be an error~

Please take care of it.


2022-09-22 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.