2019年5月4日土曜日

読書環境

開発環境

Head First はじめてのプログラミング ―頭とからだで覚えるPythonプログラミング入門 (Eric Freeman(著)、嶋田 健志(監修)、木下 哲也(翻訳)、株式会社オライリー・ジャパン)を3章(ブール型、判定、ループ - 判定コード)のコードマグネット(94ページ)の解答を求めてみる。

コード

Python 3

#!/usr/bin/env python3
import random

computer_choice = random.choice(['paper', 'rock', 'scissors'])
user_choice = input('Choce(paper/rock/scissors): ')
if computer_choice == user_choice:
    winner = 'Tie'
elif computer_choice == 'paper' and user_choice == 'rock':
    winner = 'Computer'
elif computer_choice == 'rock' and user_choice == 'scissors':
    winner = 'Computer'
elif computer_choice == 'scissors' and user_choice == 'paper':
    winner = 'Computer'
else:
    winner = 'User'

print(f'Computer: {computer_choice}, User: {user_choice}, Winner: {winner}')

入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))

C:\Users\...>py sample1.py
Choce(paper/rock/scissors): paper
Computer: paper, User: paper, Winner: Tie

C:\Users\...>py sample1.py
Choce(paper/rock/scissors): paper
Computer: paper, User: paper, Winner: Tie

C:\Users\...>py sample1.py
Choce(paper/rock/scissors): paper
Computer: scissors, User: paper, Winner: Computer

C:\Users\...>py sample1.py
Choce(paper/rock/scissors): rock
Computer: paper, User: rock, Winner: Computer

C:\Users\...>py sample1.py
Choce(paper/rock/scissors): scissors
Computer: rock, User: scissors, Winner: Computer

C:\Users\...>py sample1.py
Choce(paper/rock/scissors): scissors
Computer: scissors, User: scissors, Winner: Tie

C:\Users\...>py sample1.py
Choce(paper/rock/scissors): scissors
Computer: paper, User: scissors, Winner: User

C:\Users\...>

0 コメント:

コメントを投稿