Introductory Python Modules and conditional expressions

Introduction

You learned about conditional expressions using Python if statements. This is the study record!

Overview

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.

content of study

Getting Started with Python Conditional expression Variables and data types random module

Completion code

#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")

Code description

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.

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 statement (conditional expression)

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.

Conditional branch by 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.

① [Conditional expression 1]

if 1 <= hit < 6:

→ print ("Damage to monster" + str (hit) + "!")

Processing is executed.

② [Conditional expression 2]

elif hit >= 6:

→ print ("Critical hit damage 100 was given to the monster")

Processing is executed.

③ [Neither conditional expression 1 nor conditional expression 2 holds]

else:
print("The damage is" + str(hit) + "Defended")

Processing is executed.

Summary

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")

reference

random module https://docs.python.org/ja/3/library/random.html

Recommended Posts

Introductory Python Modules and conditional expressions
Python packages and modules
Understand Python packages and modules
About Python and regular expressions
Python tuple comprehensions and generator expressions
Python Basic Course (14 Modules and Packages)
Overlapping regular expressions in Python and Java
Modules and packages in Python are "namespaces"
difference between statements (statements) and expressions (expressions) in Python
[Python] Package and distribute your own modules
Python comprehension (list and generator expressions) [additional]
[Python] Regular Expressions Regular Expressions
Organize python modules and packages in a mess
Julia Quick Note [22] Calling Python functions and Python modules
[python] Compress and decompress
List of python modules
Python and numpy tips
[Python] pip and wheel
Batch design and python
Python iterators and generators
Vue-Cli and Python integration
Ruby, Python and map
python input and output
Python and Ruby split
Paiza Python Primer 2: Learn Conditional Branching and Comparison Operators
Get an abstract understanding of Python modules and packages
[Python] Chapter 05-01 Control syntax (comparison operator and conditional branching)
Python3, venv and Ansible
Python asyncio and ContextVar
List method argument information for classes and modules in Python
Get rid of dirty data with Python and regular expressions
Multiple regression expressions in Python
Programming with Python and Tkinter
Python Programming Workshop-Super Introductory Vol.3
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
[Python] Loading multi-level self-made modules
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Python and ruby slice memo
Python installation and basic grammar
I compared Java and Python!
Python shallow and deep copy
Use regular expressions in Python
About Python, len () and randint ()
About Python datetime and timezone
Install Python 3.7 and Django 3.0 (CentOS)
Wrap long expressions in python
Python environment construction and TensorFlow
Python class variables and instance variables
Ruby and Python syntax ~ branch ~