Paiza Python Primer 2: Learn Conditional Branching and Comparison Operators

Python3 is completely free on Paiza, so I summarized it.

Paiza Python3 Primer

01: Display a message if the numbers match

lesson.py


'''
if conditional expression:
    #Processing when the conditional expression is satisfied
'''
    
number=1

if number == 1:
    #If the left side is equal, distinguish it from substitution
    print("Love!")

lesson.py


number=2

if number == 1:
    #If the left side is equal, distinguish it from substitution
    print("Love!")

Nothing is displayed

lesson.py



number=2

if number == 1:
    #If the left side is equal, distinguish it from substitution
    print("Love!")
else:
    #Processing when the conditional expression is not satisfied
    print("I hate you!")

It is displayed as "Kirai"

lesson.py



#Randomly display likes and dislikes
import random
number=random.randint(1,2)

if number == 1:
    #If the left side is equal, distinguish it from substitution
    print("Love!")
else:
    #Processing when the conditional expression is not satisfied
    print("I hate you!")

Exercise

  1. If the ranking is 1st, try "Congratulations"

lesson.py


#Conditional branch by if statement
import random
number = random.randint(1, 3)
print("Your ranking is" + str(number) + "Is the place")
#Add an if statement here
if number ==1:
    print("Congrats")
  1. If the ranking is 2nd or lower, "a little more" is displayed.

lesson.py


#Conditional branch by if statement
import random
number = random.randint(1,5)
print("Your ranking is" + str(number) + "Is the place")
#Add an if statement here
if number == 1:
    print("Congrats")
else:
    print("A little after")
  1. Find the difference 1 Not ==

lesson.py


#Conditional branch by if statement
import random
number = random.randint(1, 3) * 100
print("Your score is" + str(number) + "Is the point")
if number == 300:
    print("Congrats")
  1. Find the difference 2 : Is not enough

lesson.py



# coding: utf-8
#Conditional branch by if statement
import random
number = random.randint(1,3) * 100
print("Your score is" + str(number) + "Is the point")

if number == 300:
    print("Congrats")
else:
    print("Zannen")

02: Let's combine multiple conditions

How to use two or more conditional expressions? → Use elif

lesson.py


#Conditional branch by if statement elif statement
number = 1
if number == 1:
	print( "Love!")	#Processing when the conditional expression is satisfied

elif conditional expression 2:
     #Processing when conditional expression 2 is satisfied

else:
	print( "I hate you")	#Processing when the conditional expression is not satisfied

Write a conditional branch "neither" in elif

lesson.py


#Conditional branch by if statement elif statement
number = 2
if number == 1:
	print( "Love!")	#Processing when the conditional expression is satisfied

elif number == 2;
        print("Neither")#Processing when conditional expression 2 is satisfied

else:
	print( "I hate you")	#Processing when the conditional expression is not satisfied

Exercise

  1. Display messages according to ranking

lesson.py


# coding: utf-8
#Conditional branch by if statement
import random
number = random.randint(1, 5)
print("Your ranking is" + str(number) + "Is the place")
#Add an if statement here
if number==1:
    print("Congrats")
elif number==2:
    print("A little after")
else :
    print("You did your best")
    
  1. Find the difference

There were many typographical errors

lesson.py


#Conditional branch by if statement
import random
number = random.randint(1, 5)
print("Your ranking is" + str(number) + "Is the place")
if number == 1:
	print("Congrats")
elif number == 2:
	print("A little after")
else:
	print("You did your best")

03 Let's branch the condition with a comparison operator

lesson.py


a == b   :a is equal to b
a > b    :a is greater than b
a < b    :a is less than b
a >= b   :a is more than b
a <= b   :a is less than or equal to b
a != b   :a is not equal to b

lesson.py


number = 2
if number > 1:
	print("Love!")	#Processing when the conditional expression is satisfied

.lesson.py


#Conditional branch by if statement Comparison operator
time = 12
if time > 1:
	print("in the morning")	#Processing when the conditional expression is satisfied
elif time ==12:
    print("noon!")
elif time >12:
    print("afternoon")

Exercise

  1. Determine if you are old enough to drink

lesson.py


import random
age = random.randint(18, 22)    #How old are 18 in age~Randomly assigned in the range of 22
text = ""
if age>= 20:
    text = "Drinkable" #Processing when the condition is met
    #Processing when the condition is met
else:
    text = "No drinking" #Processing when it was not
    #Processing when it was not
print(str(age) + "Talent" + text)
  1. Correct mistake: Determine if it is within the winning range

lesson.py


# coding: utf-8
import random
place = random.randint(1, 12)    #1 to place~Randomly assigned at 12
print(str(place) + "Rank", end="")
if place <= 6:
    print("Winning")   #Processing when the condition is met
else:
    print("Out of winning area")   #Processing when it was not

  1. Mistake correction: Adult discrimination

lesson.py


import random
age = random.randint(15, 25)    #How old are 15 in age~Randomly assign in the range of 25
print(str(age) + "Talent", end="")
if age >= 20:
    print("I'm an adult")	#Processing when the condition is met
else:
    print("I'm a minor")	#Processing when it was not

04: Let's make a fortune

lesson.py


import random

omikuji = random.randint(1,10)

print(omikuji)

if omikuji ==1:
    print("Daikichi")
elif omikuji ==2:
    print("Nakayoshi")
elif omikuji <=4:#3,4
    print("Kokichi")
elif omikuji <=7:
    print("Bad") #5,6,7
else:
    print("Great villain")

Exercise

In omikuji, numbers from 1 to 100 are random It will be substituted.

When the number of omikuji is 30 ~ 100, it is displayed as "Omikuji's contents are XX, so Daikichi". When the number of omikuji is 29 or less, it is displayed as "Omikuji's contents are XX, so it's a terrible thing."

lesson.py


import random
omikuji = random.randint(1, 100)

if omikuji >=30:
	print("The contents of omikuji" + str(omikuji) + "So Daikichi")
else:
	print("The contents of omikuji" + str(omikuji) + "So it ’s a villain")

05: Reproduce the critical hit of RPG

lesson.py



import random
hit =random.randint(1,10)
#print(hit)

if hit < 6 :
    print("To slime" + str(hit) + "Damaged")
else:
        print("critical hit!Inflicted 100 damage on slime!")

        

06: Let's find out the year of Heisei from the Christian era

lesson.py


#Find the year from the year
import datetime
seireki=2015
#seireki=atetime.date.today().If year, it will be the number subtracted from the present
print("Year" + str(seireki)+ "Year is",end="")

#Calculate the year from the year
#The year of Heisei is 1989. The difference is 1988
#Year- 1988 =Heisei*Year
#Example:Year 1989- 1988 =Heisei 1
#Example:Year 2015- 1988 =2015
heisei = seireki -1988
print("Heisei"+str(heisei) +"Year")

Exercise

  1. Let's change the year to Showa

lesson.py


#Convert the Christian era to Showa
import random
seireki = random.randint(1926, 1988) #Year
print("Year" + str(seireki) + "Year is", end = "")

#Showa year is calculated
showa = seireki-1925
#Showa year is output
print("Showa" + str(showa) + "Year")
  1. Let's convert the year to Reiwa

lesson.py



#Convert the Christian era to Reiwa
import random
ad_year = random.randint(2019, 2099) #Year
print("Year" + str(ad_year) + "Year is", end = "")

#Calculate Reiwa year
era_year = ad_year-2018
#Output Reiwa year
print("Reiwa" + str(era_year) + "Year")

Recommended Posts

Paiza Python Primer 2: Learn Conditional Branching and Comparison Operators
[Python] Chapter 05-01 Control syntax (comparison operator and conditional branching)
Paiza Python Primer 3: Learn Loop Processing
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
Paiza Python Primer 8: Understanding Classes
Paiza Python Primer 4: List Basics
About Python string comparison operators
Paiza Python Primer 7: Understanding Functions
Python 3 sorted and comparison functions
Paiza Python Primer 5: Basics of Dictionaries
Java and Python basic grammar comparison
Introductory Python Modules and conditional expressions
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
Conditional branching of Python learned by chemoinformatics
Learn Python and AI related English words. .. ..
[Python] Eliminate conditional branching by if by making full use of Enum and eval
VBA user tried using Python / R: conditional branching
Comparison of R and Python writing (Euclidean algorithm)
[Python] Learn about asynchronous programming and event loops
Comparison of Python and Ruby (Environment / Grammar / Literal)