2016年3月21日月曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Python 3.5 (プログラミング言語)

Automate the Boring Stuff with Python (Al Sweigart (著)、No Starch Press)のPart 2.(Automating Tasks)、Chapter 10.(Debugging)、Practice Projects(Debugging Coin Toss)(No. 5618)を取り組んでみる。

Practice Projects(Debugging Coin Toss)(No. 5618)

コード(Emacs)

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import random

result = ('heads', 'tails')
guess = ''
while guess not in result:
    print('Guess the coin toss! Enter heads or tails:')
    guess = input()

toss = result[random.randrange(0, 2)]
if toss == guess:
    print('You got it!')
else:
    print('Nope! Guess again!')
    guess = input()
    if toss == guess:
        print('You got it!')
    else:
        print('Nope. You are really bad at this game.')

入出力結果(Terminal, IPython)

$ ./sample.py
Guess the coin toss! Enter heads or tails:
heads
Nope! Guess again!
tails
Nope. You are really bad at this game.
$ ./sample.py
Guess the coin toss! Enter heads or tails:
heads
You got it!
$ ./sample.py
Guess the coin toss! Enter heads or tails:
heads
Nope! Guess again!
tails
You got it!
$ ./sample.py
Guess the coin toss! Enter heads or tails:
tails
Nope! Guess again!
heads
You got it!
$

0 コメント:

コメントを投稿