""" board.py Store a 3 by 3 tic-tac-toe board in a list of 3 lists. """ import sys b = [ ["X", " ", " "], [" ", "O", "O"], [" ", " ", "X"] ] print("".join(b[0])) #b[0] is a list of 3 strings. print("".join(b[1])) print("".join(b[2])) print() #Print a string of 5 lines, containing 9 pockets. print(f"""\ {b[0][0]} | {b[0][1]} | {b[0][2]} --+---+-- {b[1][0]} | {b[1][1]} | {b[1][2]} --+---+-- {b[2][0]} | {b[2][1]} | {b[2][2]} """, end = "") sys.exit(0)
X OO X X | | --+---+-- | O | O --+---+-- | | X
str
ing.
b = "X OO X"
b = "X...OO..X" #easier to understand
b = "X.." ".OO" "..X" #The 3 consecutive string literals are concatenated.
I broke the
str
ing
into three literals
so we could write them on top of each other:
#Explicit line joining. b = \ "X.." \ ".OO" \ "..X"
#Implicit line joining. b = ( "X.." #row 0 ".OO" #row 1 "..X" #row 2 )
#b is a string of 9 characters. b = ( "XX." #row 0 "XOO" #row 1 "X.O" #row 2 ) lines = [ #lines is a list of 8 strings. #3 rows b[:3], #top row b[3:6], #middle row b[6:], #bottom row #3 columns b[::3], #left column b[1::3], #middle column b[2::3], #right column #2 diagonals b[::4], #upper left to lower right b[2:8:2] #upper right to lower left ] for c in ["X", "O"]: if 3 * c in lines: print(f"{c} won.")
X won.Change the for loop to
winners = [c for c in "XO" if 3 * c in lines] #winners is a list of at most 2 strings. print("The winner(s):", *winners)
The winner(s): X
list
args
is to
unpack
it in the next very next statement.
8 * verse[1]
(without the outer
[
square
brackets]
)
would have been one very long
str
ing,
not a
list
of 8
str
ings.
""" Output the lyrics to "Old MacDonald Had a Farm". """ import sys verses = [ ["cow", "moo"], ["duck", "quack"], ["pig", "oink"], ["dog", "woof"] #etc. ] line = "Old MacDonald had a farm, E-I-E-I-O!" paragraph = """\ And on that farm he had a {}, E-I-E-I-O! With a {}-{} here and a {}-{} there, Here a {}, there a {}, Everywhere a {}-{}, """ print(line) for verse in verses: args = 8 * [verse[1]] #args is a list of 8 strings. print(paragraph.format(verse[0], *args), end = "") print(line) print() sys.exit(0)
Old MacDonald had a farm, E-I-E-I-O! And on that farm he had a cow, E-I-E-I-O! With a moo-moo here and a moo-moo there, Here a moo, there a moo, Everywhere a moo-moo, Old MacDonald had a farm, E-I-E-I-O! And on that farm he had a duck, E-I-E-I-O! With a quack-quack here and a quack-quack there, Here a quack, there a quack, Everywhere a quack-quack, Old MacDonald had a farm, E-I-E-I-O! And on that farm he had a pig, E-I-E-I-O! With a oink-oink here and a oink-oink there, Here a oink, there a oink, Everywhere a oink-oink, Old MacDonald had a farm, E-I-E-I-O! And on that farm he had a dog, E-I-E-I-O! With a woof-woof here and a woof-woof there, Here a woof, there a woof, Everywhere a woof-woof, Old MacDonald had a farm, E-I-E-I-O!
""" Output the lyrics to "If I Had a Hammer". """ import sys #The first three verses. verses = [ ["hammer", "hammer", "hammer", "Well", "hammer", "of justice"], ["bell", "ring it", "ring", "And", "bell", "of freedom"], ["song", "sing it", "sing", "And", "song to sing", "about love between my brothers and my sisters"] ] all = "All over this land." for verse in verses: print(f"If I had a {verse[0]}") print(f"I'd {verse[1]} in the morning") print(f"I'd {verse[1]} in the evening") print(all) for object in ["danger", "warning", "love between my brothers and my sisters"]: print(f"I'd {verse[2]} out {object}") print(all) print() #The fourth verse. for verse in verses: print(f"{verse[3]} I got a {verse[4]}") print(all) for _ in range(2): for verse in verses: print(f"It's the {verse[0]} {verse[5]}") print(all) sys.exit(0)
If I had a hammer I'd hammer in the morning I'd hammer in the evening All over this land. I'd hammer out danger I'd hammer out warning I'd hammer out love between my brothers and my sisters All over this land. If I had a bell I'd ring it in the morning I'd ring it in the evening All over this land. I'd ring out danger I'd ring out warning I'd ring out love between my brothers and my sisters All over this land. If I had a song I'd sing it in the morning I'd sing it in the evening All over this land. I'd sing out danger I'd sing out warning I'd sing out love between my brothers and my sisters All over this land. Well I got a hammer And I got a bell And I got a song to sing All over this land. It's the hammer of justice It's the bell of freedom It's the song about love between my brothers and my sisters All over this land. It's the hammer of justice It's the bell of freedom It's the song about love between my brothers and my sisters All over this land.