Skip to content

Commit 81c4cf2

Browse files
committed
Finished chapter 4
1 parent c5d24d1 commit 81c4cf2

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

chap04/allMyCats1.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
print('Enter the name of cat 1:')
2+
catName1 = input()
3+
print('Enter the name of cat 2:')
4+
catName2 = input()
5+
print('Enter the name of cat 3:')
6+
catName3 = input()
7+
print('Enter the name of cat 4:')
8+
catName4 = input()
9+
print('Enter the name of cat 5:')
10+
catName5 = input()
11+
print('Enter the name of cat 6:')
12+
catName6 = input()
13+
print('The cat names are:')
14+
print(catName1 + ' ' + catName2 + ' ' + catName3 + ' ' + catName4 + ' ' + catName5 + ' ' + catName6)

chap04/allMyCats2.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
catNames = []
2+
while True:
3+
print('Enter the name of cat ' + str(len(catNames) + 1) + ' (Or enter nothing to stop.):')
4+
name = input()
5+
if name == '':
6+
break
7+
catNames = catNames + [name] # list concatenation
8+
print('The cat names are:')
9+
for name in catNames:
10+
print(' ' + name)

chap04/commaList.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def commaList(items):
2+
ret = ''
3+
for item in items[:-1]:
4+
ret += item + ', '
5+
return ret + 'and ' + items[-1]
6+
7+
spam = ['apples', 'bananas', 'tofu', 'cats']
8+
print(commaList(spam))

chap04/magic8Ball2.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import random
2+
3+
messages = ['It is certain',
4+
'It is decidedly so',
5+
'Yes definitely',
6+
'Reply hazy try again',
7+
'Ask again later',
8+
'Concentrate and ask again',
9+
'My reply is no',
10+
'Outlook not so good',
11+
'Very doubtful']
12+
13+
print(messages[random.randint(0, len(messages) - 1)])

chap04/passingReference.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def eggs(someParameter):
2+
someParameter.append('Hello')
3+
4+
spam = [1, 2, 3]
5+
eggs(spam)
6+
print(spam)

chap04/printGrid.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def printGrid(grid):
2+
for y in range(6):
3+
for x in range(9):
4+
print(grid[x][y], end='')
5+
print()
6+
7+
grid = [['.', '.', '.', '.', '.', '.'],
8+
['.', 'O', 'O', '.', '.', '.'],
9+
['O', 'O', 'O', 'O', '.', '.'],
10+
['O', 'O', 'O', 'O', 'O', '.'],
11+
['.', 'O', 'O', 'O', 'O', 'O'],
12+
['O', 'O', 'O', 'O', 'O', '.'],
13+
['O', 'O', 'O', 'O', '.', '.'],
14+
['.', 'O', 'O', '.', '.', '.'],
15+
['.', '.', '.', '.', '.', '.']]
16+
printGrid(grid)

0 commit comments

Comments
 (0)