SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

[AI Terms You Sort of Know <Python for Absolute Beginners 23>] if statement × random

This time, I will introduce "if statement × random".


🔎 What is an if statement?

if means "if this happens". It gives a program the power to "change processing based on conditions."

The syntax is simple.
if condition:
process
else:
different process

  • if → If this condition is met, perform this process

  • else → If not, perform a different process

🎲 Let's make a dice game
We will roll a die using random, and decide the win or loss based on whether the number is "even or odd."

import random
dice = random.randint(1, 6)   # 1〜6のランダムな整数
if dice % 2 == 0:             # 偶数なら
print("勝ち!")
else:                         # 奇数なら
print("負け!")
  • random.randint(1, 6) → Imagine rolling a die once

  • dice % 2 == 0 → Check if the number is even

  • If even, "Win!", if odd, "Lose!"

📌 Points here

  • Use if to write "if this happens"

  • Use else to write "otherwise"

  • You can use comparisons and calculations like "==" and "%" for conditions

% is an operator that finds the remainder of a division.
If you divide the die roll by 2 and the remainder is 0, you can determine it is "even"; otherwise, it is "odd."

🤖 AI and if statements × random

The mechanism of AI is very complex, but at its root, there are "conditional branching" and "probability."
By breaking down human judgment into fine details and adding randomness to it, it achieves behavior that is closer to natural.

Even a small program like a dice game is connected to the basics of AI.

✅ Today's summary

  • An if statement is a 'conditional branch'

  • random is for 'creating randomness'

  • Combining them allows you to create game-like mechanisms

Practice Problems

1. Fortune-telling Game 🌸

This is a program where the computer chooses one from 'Great Blessing', 'Blessing', and 'Curse' and displays it.

Problem:

  1. Use random.randint() to generate a random integer from 1 to 3.

  2. Write a program that displays 'Great Blessing' if the integer is 1, 'Blessing' if it is 2, and 'Curse' if it is 3.

import random
omikuji = random.randint(1, 3)
if omikuji == 1:
print("大吉!")
elif omikuji == 2:
print("吉!")
else:
print("凶!")

Tip:

  • elif is short for else if, and it checks the next condition if the condition of the first if statement is not met.

  • The final else is executed if none of the if or elif conditions are met, which means when omikuji is 3.

2. Rock-Paper-Scissors Game ✊

This is a program where you play rock-paper-scissors against the computer.

Problem:

  1. You input either 'Rock (1)', 'Scissors (2)', or 'Paper (3)'.

  2. The computer chooses a random number from 1 to 3 using random.randint().

  3. Compare your move with the computer's move and display whether it is a 'Win', 'Loss', or 'Draw'.


import random 
cpu_hand = random.randint(1, 3)
my_hand = int(input("あなたの手を選んでください: グー(1), チョキ(2), パー(3) -> "))
if my_hand == cpu_hand:
print("あいこ!")
elif (my_hand == 1 and cpu_hand == 2) or\
(my_hand == 2 and cpu_hand == 3) or\
(my_hand == 3 and cpu_hand == 1):
print("あなたの勝ち!")
else:
print("あなたの負け!")

Tip:

  • The input() function treats the entered content as a string. Since it needs to be converted to an integer for comparison, we use int().

  • By using 'or' and 'and' to combine multiple conditions, complex win/loss judgments are consolidated into a single if statement.


いいなと思ったら応援しよう!

こばみお|生成AIとおしゃべりしながらの、50代のゆるチャレンジ。 よろしければ応援お願いします!いただいたチップはクリエイターとしての活動に使わせていただきます。