0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ChatGPTがpythonで書いた「じゃんけん」のコード

Posted at

Pythonでじゃんけんのプログラムを書く方法はいくつかありますが、以下にシンプルな例を示します。

janken.py
import random

def janken(player_choice):
    options = ["グー", "チョキ", "パー"]
    computer_choice = random.choice(options)
    
    print(f"あなたの選択: {player_choice}")
    print(f"コンピュータの選択: {computer_choice}")

    if player_choice == computer_choice:
        print("引き分けです。")
    elif (player_choice == "グー" and computer_choice == "チョキ") or \
         (player_choice == "チョキ" and computer_choice == "パー") or \
         (player_choice == "パー" and computer_choice == "グー"):
        print("あなたの勝ちです!")
    else:
        print("コンピュータの勝ちです。")

player_choice = input("じゃんけん: グー、チョキ、パー のいずれかを入力してください: ")
janken(player_choice)

このプログラムでは、janken() 関数を定義しています。この関数は、プレイヤーの選択("グー"、"チョキ"、"パー")を引数として受け取り、コンピュータの選択をランダムに決定します。その後、両者の選択を表示し、結果を判定して勝敗を表示します。

プログラムの最後に、input() 関数を使ってプレイヤーに選択を入力させ、janken() 関数を呼び出してじゃんけんを実行しています。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?