Please refer to here for notes.
History repeats itself.
Let's repeat a simple program.
Simple iterative process.
for counter variable in repeat range:
Iterative processing
for i in range(3):
print("hello world")
============ ※ Execution result
hello world hello world hello world
============
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(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
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
============
Numerical values can also be repeated.
for i in range(10):
print(i)
============ ※ Execution result
1
2
3
4
5
6
7
8
9
============
for i in range(1,13):
print(str(i) + "Month")
============ ※ Execution result
January
February
March
April
May
June
July
August
September
October
November
December
============
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
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))
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 |
i = 5
while i <= 10:
print(i)
i += 1
============ ※ Execution result
5
6
7
8
9
10
============
i = 5
while i >= 1:
print(i)
i -= 1
============ ※ Execution result
5
4
3
2
1
============
import random
hp = 30
while hp > 0:
hit = random.randint(1,10)
print("To slime" + str(hit) + "Damaged!")
hp -= hit
print("Defeated slime")
============
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
============
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")
============
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
============
Recommended Posts