46 Capítulo 1
o código
Código do Catulo 1
from datetime import datetime
odds = [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
21, 23, 25, 27, 29, 31, 33, 35, 37, 39,
41, 43, 45, 47, 49, 51, 53, 55, 57, 59 ]
right_this_minute = datetime.today().minute
if right_this_minute in odds:
print("This minute seems a little odd.")
else:
print("Not an odd minute.")
from datetime import datetime
import random
import time
odds = [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
21, 23, 25, 27, 29, 31, 33, 35, 37, 39,
41, 43, 45, 47, 49, 51, 53, 55, 57, 59 ]
for i in range(5):
right_this_minute = datetime.today().minute
if right_this_minute in odds:
print("This minute seems a little odd.")
else:
print("Not an odd minute.")
wait_time = random.randint(1, 60)
time.sleep(wait_time)
word = "bottles"
for beer_num in range(99, 0, -1):
print(beer_num, word, "of beer on the wall.")
print(beer_num, word, "of beer.")
print("Take one down.")
print("Pass it around.")
if beer_num == 1:
print("No more bottles of beer on the wall.")
else:
new_num = beer_num - 1
if new_num == 1:
word = "bottle"
print(new_num, word, "of beer on the wall.")
print()
Começamos com
o programa
odd.py”, então...
... estendemos o código
para criar “odd2.py”,
que executou o “código
de verificação do
minuto” cinco vezes
(graças ao uso do loop
for” do Python).
Concluímos o
capítulo com a
versão Python da
clássica "canção
da cerveja" do
Use a Cabeça. E
sim, sabemos: é
difícil trabalhar
no código sem
cantar junto... §
CG_HeadFirst_Python.indb 46 18/07/2018 13:18:02

Get USE A CABEÇA! PYTHON now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.