# Introduction

Please refer to here for notes.

# Iterative processing

History repeats itself.
Let's repeat a simple program.

## Loop processing by for in

Simple iterative process.

``````for counter variable in repeat range:
Iterative processing
``````
• Be careful not to forget the colon

#### Example

``````for i in range(3):
print("hello world")

``````

============ ※ Execution result

hello world hello world hello world

============

#### Range function to repeat

The range function creates a range type object that has consecutive numerical values from the start number to the end number specified in the argument as elements.

• Range (a, b, x): Creates an object that has consecutive numerical values as elements up to the range that does not exceed the numerical value specified in x by adding the numerical values specified in b in order from the numerical value specified in a.
-A and x can be omitted. In that case, a = 0 and x = 1.
``````range(5)
# 0 1 2 3 4

range(0, 5)
# 0 1 2 3 4

range(4,7)
# 4 5 6

range(0, 5, 1)
# 0 1 2 3 4

range(0, 10, 2)
# 0 2 4 6 8

range(10, 0, -1)
# 10 9 8 7 6 5 4 3 2 1

range(0, -8, -2)
# 0 -2 -4 -6
``````
##### Try to specify with the range function
``````for i in range(6,11): # 6, 7, 8 ... 11
print("Hello World:" + str(i)) #Display a numerical value behind
print("last " + str(i)) #For confirmation

``````

============ ※ Execution result

Hello World:6 Hello World:7 Hello World:8 Hello World:9 Hello World:10 last 10

============

### Repeat the numbers

Numerical values can also be repeated.

``````for i in range(10):
print(i)

``````

============ ※ Execution result

1
2
3
4
5
6
7
8
9

============

#### (Example) Display from January to December

``````for i in range(1,13):
print(str(i) + "Month")
``````

============ ※ Execution result

January
February
March
April
May
June
July
August
September
October
November
December

============

## Iterative processing by while

A process that is repeated many times while the conditional expression is satisfied. You need to prepare the counter variables and conditional expressions yourself.

``````#while conditional expression:
#Iterative processing
#Update counter variable

``````
• Be careful not to forget the colon

#### Example

Add the numbers and repeat until the number is less than or equal to the specified number or more than the specified number.

``````i = 1   #Initialization of counter variables
while i <= 10: #Execute if i is 10 or less(1,2,3,4...10)
print(i)   #Iterative processing
i = i + 1  #Updated counter variables. i= i +Repeat from 1 to 10
print("Next:" + str(i)) # Next: 11
print("Last:" + str(i))
``````
• If there is no update statement for the counter variable, it becomes a infinite loop </ b> (statement of "i = i + 1").
##### Regarding the update expression (assignment operator) of the counter variable
operator Example meaning Another way of writing
+= i += 1 Increase the value of variable i by 1. a = a + 1
-= i -= 1 Increase the value of variable i by 1. a = a - 1

#### example

##### Display from 5 to 15
``````i = 5
while i <= 10:
print(i)
i += 1
``````

============ ※ Execution result

5
6
7
8
9
10

============

##### Countdown from 5 to 1
``````i = 5
while i >= 1:
print(i)
i -= 1
``````

============ ※ Execution result

5
4
3
2
1

============

##### Attack until the monster's HP reaches 0
``````import random
hp = 30
while hp > 0:
hit = random.randint(1,10)
print("To slime" + str(hit) + "Damaged!")
hp -= hit
print("Defeated slime")
``````

============

• Execution result (example)

Inflicted 2 damage on slime! Inflicted 9 damage on slime! Inflicted 7 damage on slime! Inflicted 8 damage on slime! Inflicted 4 damage on slime! Defeated slime

============

##### Repeat until full recovery
``````import random
hp = 1
while hp <= 150:
kusa = random.randint(10,30)
print("Use herbs to increase HP" + str(kusa) + "I recovered." + "Current HP:" + str(hp))
hp += kusa
print("Full recovery of physical strength")
``````

============

• Execution result (example)

Recovered 18 HP using herbs. Current HP: 1 Recovered 15 HP using herbs. Current HP: 19 Recovered 29 HP using herbs. Current HP: 34 Recovered 15 HP using herbs. Current HP: 63 Recovered 18 HP using herbs. Current HP: 78 Recovered 12 HP using herbs. Current HP: 96 Recovered 15 HP using herbs. Current HP: 108 Recovered 14 HP using herbs. Current HP: 123 Recovered 14 HP using herbs. Current HP: 137 Full recovery of physical strength

============

Paiza Learning