Contents
6 While Loop
i = 1
while i <= 10:
print(i)
i = i + 1 # or i+= 1
print("Done with Loop")
1
2
3
4
5
6
7
8
9
10
Done with Loop
17 Building a Guessing Game
secret_word = "giraffe"
guess = ""
while guess != secret_word:
guess = input("Enter guess: ")
print("You win!")
Enter guess: text
Enter guess: room
Enter guess: house
Enter guess: giraffe
You win!
secret_word = "giraffe"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess != secret_word and not(out_of_guesses):
if guess_count < guess_limit:
guess = input("Enter guess: ")
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print("Out of guesses, You lose!")
else:
print("You win!")
Enter guess: text
Enter guess: house
Enter guess: room
Out of guesses, You lose!
Enter guess: sdsds
Enter guess: sdsdsd
Enter guess: giraffe
You win!
18 For Loop
# letter
for letter in "Giraffe Academy":
print(letter)
# char
friends = ["Jim", "Karen", "Kevin"]
for name in friends:
print(name)
# num
for index in range(10):
print(index)
for index in range(3, 10):
print(index)
G
i
r
a
f
f
e
A
c
a
d
e
m
y
Jim
Karen
Kevin
0
1
2
3
4
5
6
7
8
9
3
4
5
6
7
8
9
friends = ["Jim", "Karen", "Kevin"]
len(friends)
for index in range(len(friends)):
print(index)
0
1
2
friends = ["Jim", "Karen", "Kevin"]
len(friends)
for index in range(len(friends)):
print(friends[index])
Jim
Karen
Kevin
friends = ["Jim", "Karen", "Kevin"]
for index in range(5):
if index == 0:
print("first Iteration")
else:
print("Not first")
first Iteration
Not first
Not first
Not first
Not first
19 Exponent Function
def raise_to_power(base_num, pow_num):
result = 1
for index in range(pow_num):
result = result * base_num
return result
print(raise_to_power(3, 2))
9
print(raise_to_power(3, 4))
81
20 2D Lists and Nested Loops
number_grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]
print(number_grid)
print(number_grid[0][0])
print(number_grid[2][1])
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [0]]
1
8
number_grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]
for row in number_grid:
print(row)
for row in number_grid:
for col in row:
print(col)
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[0]
1
2
3
4
5
6
7
8
9
0
21 Build a Translator
# Giraffe Language
# vowels -> g (a, e, i, o, u)
# ----------
# eg:
# dog -> dgg
# cat -> cgt
def translate(phrase):
translation = ""
for letter in phrase:
if letter in "AEIOUaeiou": # or if letter.lower() in "aeiou":
translation = translation + "g"
else:
translation = translation + letter
return translation
print(translate(input("Enter a prase: ")))
Enter a prase: dog
Dgg
Enter a prase: to be or not to be
tg bg gr ngt tg bg
# Giraffe Language
# vowels -> g (a, e, i, o, u)
# ----------
# eg:
# dog -> dgg
# cat -> cgt
def translate(phrase):
translation = ""
for letter in phrase:
if letter.lower() in "aeiou":
if letter.isupper():
translation = translation + "G"
else:
translation = translation + "g"
else:
translation = translation + letter
return translation
print(translate(input("Enter a prase: ")))
Enter a prase: On
Gn
22 Comments
# Comment This PROGRAM is cool
# saisdjaidjsiadjis
'''
sdsdsdsdsdsds
sdsdsdsd
sdsdsdsd
dsdsd
sdsdsdsdsddsds
'''
# print("Comments are fun")
print("Comments are fun")
Comments are fun
23 Try and Except 允许出错
number = int(input("Enter a number: "))
print(number)
# print(int(input("Enter a number: ")))
Enter a number: 2
2
Enter a number: sadsad
Traceback (most recent call last):
File "C:\Users\Chengzhi Zhang\PycharmProjects\pythonProject3\practice01.py", line 2, in <module>
number = int(input("Enter a number: "))
ValueError: invalid literal for int() with base 10: 'sadsad'
try:
number = int(input("Enter a number: "))
print(number)
except:
print("Invalid Input")
Enter a number: insdi
Invalid Input
try:
value = 10 / 0
number = int(input("Enter a number: "))
print(number)
except:
print("Invalid Input")
Invalid Input
try:
number = int(input("Enter a number: "))
print(number)
except ZeroDivisionError:
print("Divided by zero")
except ValueError:
print("Invalid Input")
try:
answer = 10/0
number = int(input("Enter a number: "))
print(number)
except ZeroDivisionError as err:
print("err")
except ValueError:
print("Invalid Input")
err