LoginSignup
0
0

More than 3 years have passed since last update.

じゃんけん 

Posted at

script.py
import utils

randomモジュールを読み込んでください

import random

print('じゃんけんをはじめます')
player_name = input('名前を入力してください:')
print('何を出しますか?(0: グー, 1: チョキ, 2: パー)')
player_hand = int(input('数字で入力してください:'))

if utils.validate(player_hand):
# randintを用いて0から2までの数値を取得し、変数computer_handに代入してください
computer_hand = random.randint(0,2)

if player_name == '':
    utils.print_hand(player_hand)
else:
    utils.print_hand(player_hand, player_name)

utils.print_hand(computer_hand, 'コンピューター')

result = utils.judge(player_hand, computer_hand)
print('結果は' + result + 'でした')

else:
print('正しい数値を入力してください')


utils.py
def validate(hand):
if hand < 0 or hand > 2:
return False
return True

def print_hand(hand, name='ゲスト'):
hands = ['グー', 'チョキ', 'パー']
print(name + 'は' + hands[hand] + 'を出しました')

def judge(player, computer):
if player == computer:
return '引き分け'
elif player == 0 and computer == 1:
return '勝ち'
elif player == 1 and computer == 2:
return '勝ち'
elif player == 2 and computer == 0:
return '勝ち'
else:
return '負け'

0
0
1

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