You learned about conditional expressions using Python if statements. This is the study record!
I'm learning Python. I learned the basic conditional expression by reproducing the "competition situation of RPG game", so I added an arrangement and summarized it.
Getting Started with Python Conditional expression Variables and data types random module
#Reproduce the attack situation of RPG
#Fighting monsters.
#Roll 1 to 10 dice,
#If it is less than 6, it is displayed that only the dice have been damaged.
#If it is 6 or more, it is displayed as a critical hit and 100 damage is done.
#If it is 0, 0 damage is displayed as protected.
import random
hit = random.randint(0,10)
if 1 <= hit < 6:
print("To slime" + str(hit) + "Damaged!")
elif hit >= 6:
print("Inflicted 100 critical hit damage")
else:
print("The damage is" + str(hit) + "Defended")
Let's check the code one by one. import
import random
hit = random.randint(0,10)
Import means to incorporate.
import random
In this case, you have included the random module.
A module is a part for designing programming. In Python, it seems that programs are created by combining these modules.
The random module used this time contains "random function", "randint function", etc.
When using the random module, use it with "random.function name ()".
Example) random.random () random.randint()
hit = random.randint(0,10)
Therefore, in this code, a number is randomly assigned to the variable called hit from the values 0 to 10 specified by the randint function.
if 1 <= hit < 6:
print("To monsters" + str(hit) + "Damaged!")
elif hit >= 6:
print("Inflicted 100 critical hit damage on monsters")
else:
print("The damage is" + str(hit) + "Defended")
The following code is a conditional expression. First, let's check how to write a conditional branch using a basic if statement.
Here is how to write a conditional branch with a basic if statement.
number = 1
① if conditional expression 1:
print( "OK!") #Processing when conditional expression 1 is satisfied
② elif conditional expression 2:
print( "Neither") #Processing when conditional expression 2 is satisfied
③else:
print( "NG!") #Processing when none of the conditional expressions are satisfied
In this conditional expression,
Processing is performed on the condition that the value from 0 to 10 specified by the randint function assigned to hit is used.
I will apply it.
if 1 <= hit < 6:
→ print ("Damage to monster" + str (hit) + "!")
Processing is executed.
elif hit >= 6:
→ print ("Critical hit damage 100 was given to the monster")
Processing is executed.
else:
print("The damage is" + str(hit) + "Defended")
Processing is executed.
that's all.
Conditional branching is performed according to the randomly generated value, and the process is executed.
I tried to reproduce the series of flow as "competition situation of RPG game".
import random
hit = random.randint(0,10)
if 1 <= hit < 6:
print("To slime" + str(hit) + "Damaged!")
elif hit >= 6:
print("Inflicted 100 critical hit damage")
else:
print("The damage is" + str(hit) + "Defended")
random module https://docs.python.org/ja/3/library/random.html
Recommended Posts