2014年9月7日日曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 9(Repeating Code Using Loops)、9.10(Exercises) 6.を解いてみる。

9.10(Exercises) 6.

コード(BBEdit)

sample6.py

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

text = ''
while text.lower() != 'quit':
    text = input("Please enter a chemical formula (or 'quit' to exit): ")
    if text.lower() == 'quit':
        print('.…exiting program')
    elif text == 'H2O':
        print('Water')
    elif text == 'NH3':
        print('Ammonia')
    elif text == 'CH4':
        print('Methane')
    else:
        print('Unknown compound')

入出力結果(Terminal, IPython)

$ ./sample6.py
Please enter a chemical formula (or 'quit' to exit): quit
.…exiting program
$ ./sample6.py
Please enter a chemical formula (or 'quit' to exit): Quit
.…exiting program
$ ./sample6.py
Please enter a chemical formula (or 'quit' to exit): QUIT
.…exiting program
$

0 コメント:

コメントを投稿