Skip to content

Commit c5d24d1

Browse files
committed
Finished chapter 3
1 parent 1c472f4 commit c5d24d1

10 files changed

+122
-0
lines changed

chap03/collatz.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def collatz(number):
2+
if number % 2 == 0:
3+
number = number // 2
4+
else:
5+
number = 3 * number + 1
6+
print(number)
7+
return number
8+
9+
try:
10+
number = int(input("Input a positive integer: "))
11+
while number != 1:
12+
number = collatz(number)
13+
except ValueError:
14+
print("You should input a positive integer.")

chap03/guessTheNumber.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This is a guess the number game.
2+
import random
3+
secretNumber = random.randint(1, 20)
4+
print('I am thinking of a number between 1 and 20.')
5+
6+
# Ask the player to guess 6 times.
7+
for guessesTaken in range(1, 7):
8+
print('Take a guess.')
9+
guess = int(input())
10+
11+
if guess < secretNumber:
12+
print('Your guess is too low.')
13+
elif guess > secretNumber:
14+
print('Your guess is too high.')
15+
else:
16+
break # This condition is the correct guess!
17+
18+
if guess == secretNumber:
19+
print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
20+
else:
21+
print('Nope. The number I was thinking of was ' + str(secretNumber))

chap03/helloFunc.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def hello():
2+
print('Howdy!')
3+
print('Howdy!!!')
4+
print('Hello there.')
5+
6+
hello()
7+
hello()
8+
hello()

chap03/helloFunc2.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def hello(name):
2+
print('Hello ' + name)
3+
4+
hello('Alice')
5+
hello('Bob')

chap03/magic8Ball.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import random
2+
3+
def getAnswer(answerNumber):
4+
if answerNumber == 1:
5+
return 'It is certain'
6+
elif answerNumber == 2:
7+
return 'It is decidely decidedly so'
8+
elif answerNumber == 3:
9+
return 'Yes'
10+
elif answerNumber == 4:
11+
return 'Reply hazy try again'
12+
elif answerNumber == 5:
13+
return 'Ask again later'
14+
elif answerNumber == 6:
15+
return 'Concentrate and ask again'
16+
elif answerNumber == 7:
17+
return 'My reply is no'
18+
elif answerNumber == 8:
19+
return 'Outlook not so good'
20+
elif answerNumber == 9:
21+
return 'Very doubtful'
22+
23+
r = random.randint(1, 9)
24+
fortune = getAnswer(r)
25+
print(fortune)

chap03/sameName.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def spam():
2+
eggs = 'spam local'
3+
print(eggs) # prints 'spam local'
4+
5+
def bacon():
6+
eggs = 'bacon local'
7+
print(eggs) # prints 'bacon local'
8+
spam()
9+
print(eggs) # prints 'bacon local'
10+
11+
eggs = 'global'
12+
bacon()
13+
print(eggs) # prints 'global'

chap03/sameName2.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def spam():
2+
global eggs
3+
eggs = 'spam'
4+
5+
eggs = 'global'
6+
spam()
7+
print(eggs)

chap03/sameName3.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def spam():
2+
global eggs # this is the global
3+
eggs = 'spam'
4+
5+
def bacon():
6+
eggs = 'bacon' # this is a local
7+
8+
def ham():
9+
print(eggs) # this is the global
10+
11+
eggs = 42 # this is the global
12+
spam()
13+
print(eggs)

chap03/sameName4.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def spam():
2+
print(eggs) # ERROR!
3+
eggs = 'spam local'
4+
5+
eggs = 'global'
6+
spam()

chap03/zeroDivide.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def spam(divideBy):
2+
return 42 / divideBy
3+
4+
try:
5+
print(spam(2))
6+
print(spam(12))
7+
print(spam(0))
8+
print(spam(1))
9+
except ZeroDivisionError:
10+
print('Error: Invalid argument.')

0 commit comments

Comments
 (0)