0
0

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 5 years have passed since last update.

Python学習ノート_005

Last updated at Posted at 2019-12-16

サンプルのコードを読む前に自分の考えで書いたコードです。

  • ポイント
  • 文字列を比較する場合は下記の方法があります。
    • 完全一致(等価): ==, !=
    • 部分一致: in, not in
    • 前方一致・後方一致(先頭・末尾): startswith(), endswith()
  • if条件に複数条件(and orなど)ある場合、バックスラッシュ(\)を使えば何度改行してもいい。同じく、括弧()内であれば何度改行してもいい。インデントの制限もない。
sample03.py
import random

# じゃんけんの定義
choices = ['グー', 'パー', 'チョキ']

str_gu = "グー"
str_pa = "パー"
str_chyoki = "チョキ"

icon_gu = ""
icon_pa = ""
icon_chyoki = "✌️"

people_win = "人の勝ち"
computer_win = "パソンコンの勝ち"

# パソコンの選択を作る
computer_choice = random.choice(choices)

# 人の選択を聞く
people_choice = input("あなたの選択はなんですか。「グー、パー、チョキ」から1つを選択してください。 ")

# 人が一部のみ入力する場合:
if (people_choice.startswith("g") or
    people_choice.startswith("") or
    people_choice.startswith("") ) :
    people_choice = str_gu

if (people_choice.startswith("p") or
    people_choice.startswith("") or
    people_choice.startswith("") ) :
    people_choice = str_pa

if (people_choice.startswith("c") or
    people_choice.startswith("") or
    people_choice.startswith("")) :
    people_choice = str_chyoki
    

# アイコンを設定する
computer_icon = ""
people_icon = ""
if computer_choice == str_gu :
    computer_icon = icon_gu
elif computer_choice == str_pa :
    computer_icon = icon_pa
elif computer_choice == str_chyoki :
    computer_icon = icon_chyoki

if people_choice == str_gu :
    people_icon = icon_gu
elif people_choice == str_pa :
    people_icon = icon_pa
elif people_choice == str_chyoki :
    people_icon = icon_chyoki

# パソコンの選択を出力する
if people_icon == "" :
    print("入力は間違いました。再度さいしょからやり直してください。")
    
# パソコンの選択を出力する
print("パソコンの選択は",
      computer_choice,
      computer_icon)
print("人の選択は",
      people_choice,
      people_icon)

# 判断
# 1.一致する場合、
if computer_choice == people_choice :
    print("再度じゃんけんしてください。")
elif computer_choice == str_gu and people_choice == str_pa :
    print(people_win)
elif computer_choice == str_pa and people_choice == str_chyoki :
    print(people_win)
elif computer_choice == str_chyoki and people_choice == str_gu :
    print(people_win)
else :
    print(computer_win)
  • 実行結果
    残念ですが、パソコンに負けました。
    じゃんけんの結果
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?