計算機科学のブログ

ほしい物リスト

Python - List of Files: Functions, Modules and Files - else statement

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 4.(List of Files: Functions, Modules & Files)、EXERCISE(237/682)の解答を求めてみる。

Jupyter(コード、入出力結果)

Files.ipynb

t = '1:23.45'
if ':' in t:
    minutes, rest = t.split(':')
    seconds, hundredths = rest.split('.')
else:
    minutes = 0
    seconds, hundredths = t.split('.')
minutes, seconds, hundredths
('1', '23', '45')
t = '23.45'
if ':' in t:
    minutes, rest = t.split(':')
    seconds, hundredths = rest.split('.')
else:
    minutes = 0
    seconds, hundredths = t.split('.')
minutes, seconds, hundredths
(0, '23', '45')