Py じゃんけんコード

未分類

コード

import random

def janken():
    # ジャンケンの手の選択肢
    choices = ['グー', 'チョキ', 'パー']
    
    # コンピュータの手をランダムに選択
    computer_choice = random.choice(choices)
    
    # ユーザーに入力を促す
    user_choice = input("ジャンケンの手を選んでください(グー、チョキ、パー): ")
    
    # 正しい入力かどうかをチェック
    if user_choice not in choices:
        print("無効な入力です。もう一度入力してください。")
        return
    
    # 結果を表示
    print(f"あなたの手: {user_choice}")
    print(f"コンピュータの手: {computer_choice}")
    
    # 勝敗の判定
    if user_choice == computer_choice:
        print("あいこです!")
    elif (user_choice == 'グー' and computer_choice == 'チョキ') or \
         (user_choice == 'チョキ' and computer_choice == 'パー') or \
         (user_choice == 'パー' and computer_choice == 'グー'):
        print("あなたの勝ちです!")
    else:
        print("コンピュータの勝ちです!")

# ゲームを開始
janken()

結果

ジャンケンの手を選んでください(グー、チョキ、パー): チョキ
あなたの手: チョキ
コンピュータの手: グー
コンピュータの勝ちです!

コメント

タイトルとURLをコピーしました