開発環境
- macOS High Sierra - Apple
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming (Bryson Payne(著)、No Starch Press)のChapter 7.(Functions: There's a Name for That)、PROGRAMMING CHALLENGES、#2: More Ping-Pong Calculationsを取り組んでみる。
#2: More Ping-Pong Calculations
コード(Emacs)
Python 3
#!/usr/bin/env python3 import random import turtle def convert_cm2in(cm): return cm / 2.54 def convert_kg2lb(kg): return kg * 2.2 ping_pong_balls = int(input('Enter a number of Ping-Pong balls: ')) height_cm = ping_pong_balls * 4 weight_kg = ping_pong_balls * 2.7 / 1000 height_in = round(convert_cm2in(height_cm)) weight_lb = round(convert_kg2lb(weight_kg)) feet = height_in // 12 inch = height_in % 12 print(f'At {feet} feet {inch} inches tall, and {weight_lb} pounds, ')
入出力結果(Terminal, Jupyter(IPython))
$ ./sample2.py Enter a number of Ping-Pong balls: 100 At 13 feet 1 inches tall, and 1 pounds, $ ./sample2.py Enter a number of Ping-Pong balls: 1000 At 131 feet 3 inches tall, and 6 pounds, $
0 コメント:
コメントを投稿