Skip to content

Commit 22265ec

Browse files
committed
add exercise files for lessons 1-3
1 parent 2c9398b commit 22265ec

File tree

15 files changed

+469
-0
lines changed

15 files changed

+469
-0
lines changed

advanced/lesson1/diagnostic.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -- Part 1: What is the output? --
2+
3+
# 1.
4+
foo = "bar"
5+
print(foo)
6+
print("foo")
7+
8+
9+
10+
#2.
11+
a = True
12+
b = False
13+
c = False
14+
15+
if (a or b) and c:
16+
print("coffee")
17+
else:
18+
print("tea")
19+
20+
21+
22+
#3.
23+
def mul(a,b):
24+
return a * b
25+
print(a*b == mul(a,b))
26+
27+
# -- Part 2: Loops --
28+
29+
# 1.
30+
# Print each element of the list
31+
lis = ["Interior", "Crocodile", "Alligator", "Chevrolet"]
32+
for i in range(len(lis)):
33+
# Your code here:
34+
35+
36+
37+
# 2.
38+
# End the loop using ‘break’ if the age is less than 18
39+
age = 0
40+
while age < 18:
41+
age = int(input("What is your age? "))
42+
# Your code here
43+
44+
45+
46+
# 3.
47+
# Set x and y so that the loop prints the numbers
48+
# 1, 2, 3, and 4 ONLY.
49+
50+
# Your code here
51+
x =
52+
y =
53+
for i in range(x, y):
54+
print(i)
55+
56+
# -- Part 3: Getting Functional --
57+
# Write a function (also called ‘method’) to perform the indicated operation.
58+
59+
# 1.
60+
'''
61+
Write a function that takes one number, n, as a
62+
parameter and returns the sum of all numbers from 1
63+
to n, inclusive.
64+
65+
e.g.
66+
sum_to(1) -> 1
67+
sum_to(3) -> 1 + 2 + 3 -> 6
68+
sum_to(10) -> 55
69+
'''
70+
# Your code here
71+
72+
73+
74+
75+
76+
# 2.
77+
'''
78+
Write a function that counts the number
79+
of vowels in a word.
80+
81+
Vowels are any of a, e, i, o, and u
82+
83+
e.g.
84+
count_vowels("abc") -> 1
85+
count_vowels("aeiou") -> 5
86+
count_vowels("abracadabra") -> 5
87+
'''
88+
# Your code here
89+
90+
i = 0
91+
while i < 5:
92+
print(i)
93+
i += 1

advanced/lesson1/hello.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello, world!")

advanced/lesson1/practice.py

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
'''
2+
This file includes practice code that should refresh
3+
your memory on basic Python concepts. If anything here
4+
looks totally out of wack, ask an instructor for help!
5+
6+
Remove the triple quotes around each code block to run it
7+
'''
8+
# ~~ Variables and I/O ~~
9+
'''
10+
x = 9
11+
x += 2 # x is now 11
12+
print(x)
13+
14+
a = "strings can be"
15+
b = " added "
16+
c = "like this!"
17+
print(a + b + c)
18+
19+
# Get input from the user, click into the terminal to respond
20+
name = input("Hi there, what is your name? ")
21+
print("Thanks for coming, " + name + "!")
22+
'''
23+
24+
# ~~ If Statements and Logic ~~
25+
'''
26+
a = True
27+
b = False
28+
29+
# Not 'flips' a boolean variable
30+
print("a and b flipped are:")
31+
print('a: ' + str(not a))
32+
print('b: ' + str(not b))
33+
34+
# An if statement can have two branching paths
35+
# Don't forget the colons!
36+
if a:
37+
print("a is true")
38+
else:
39+
print("a is false")
40+
41+
# Add more outcomes with elif
42+
if a:
43+
print("a is still true")
44+
elif b:
45+
print("a is false but b is true")
46+
else:
47+
print("nothing is true")
48+
49+
# and, not, and or make if statements more complex
50+
if a or b:
51+
print("One of a and b are true")
52+
if a and b:
53+
print("Both a and b are true")
54+
if not (a or b):
55+
print("Neither a nor b are true")
56+
'''
57+
58+
# ~~ Loops ~~
59+
'''
60+
# There are two flavors of loops: for and while
61+
62+
# For loops execute a set number of times
63+
print("Numbers from 1-10 (for loop)")
64+
for i in range(11):
65+
print(i, end=" ")
66+
print()
67+
# While loops execute as long as a condition is true
68+
i = 0
69+
print("Numbers from 1-10 (while loop)")
70+
while i <= 10:
71+
print(i, end=" ")
72+
i += 1 # What if this was multiplication by 2?
73+
print()
74+
# A more complex while loop
75+
# 'break' tells Python to end the loop prematurely
76+
print("Some more numbers")
77+
i = 1
78+
while i < 6:
79+
print(i, end=" ")
80+
if i == 3:
81+
break
82+
i += 1
83+
84+
print()
85+
'''
86+
87+
# ~~ Functions ~~
88+
'''
89+
# Functions collect code with a common purpose in one block
90+
def star_staircase(steps):
91+
"""
92+
I'm a comment that says what the function does
93+
94+
It makes a stair case of stars, like so:
95+
star_staircase(3) ->
96+
*
97+
**
98+
***
99+
"""
100+
101+
for i in range(steps):
102+
print(" " * (steps - i - 1), end="")
103+
print("*" * (i+1))
104+
105+
star_staircase(3)
106+
star_staircase(6)
107+
108+
# star_staircase doesn't return a value, but this one does
109+
def pig_latin(string):
110+
"""
111+
Translates string to pig latin under the following rules:
112+
- If first letter is a vowel, simply add 'ay'
113+
- If first letter is consonant, put it at the end of the word and add
114+
'ay'
115+
"""
116+
vowels = 'aeiou'
117+
ay = 'ay'
118+
if string[0].lower() not in vowels:
119+
string += string[0]
120+
string = string[1:]
121+
122+
return string.lower().capitalize() + ay
123+
124+
print(pig_latin("Hello"))
125+
print(pig_latin("Please"))
126+
'''

advanced/lesson3/files.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
File I/O exercises.
3+
'''
4+
5+
'''
6+
# 1. Write a loop to write each string in the list
7+
# to the file.
8+
9+
# To make a new line, use '\n' e.g. file.write('foo\n')
10+
11+
to_write = ["All", "my", "favorite", "things"]
12+
with open('write.txt', 'w') as file:
13+
# Your code here
14+
15+
print("Finished exercise 1")
16+
'''
17+
18+
'''
19+
# 2. Read each line from read.txt and print its length
20+
# on screen.
21+
# Expected output:
22+
# ---------------------
23+
# 2
24+
# 3
25+
# 4
26+
27+
with open('read.txt', 'r') as file:
28+
# Your code here
29+
30+
print("Finished exercise 2")
31+
'''
32+
33+
'''
34+
# 3. Write each line that starts with an 'a' in mix.txt
35+
# to only_a.txt
36+
37+
# Remember, make a new line by adding '\n' e.g. file.write(line + '\n')
38+
39+
to_write = []
40+
with open('mix.txt', 'r') as file:
41+
# Your code here
42+
'''
43+
44+
# Don't modify this code, it checks your work in exercise 3.
45+
46+
success = True
47+
line_seen = False
48+
with open('only_a.txt', 'r') as file:
49+
for line in file:
50+
line_seen = True
51+
if line[0] != 'a':
52+
success = False
53+
break
54+
55+
if success and line_seen:
56+
print("Exercise 3 correct and finished!")
57+
else:
58+
print("Something not right with exercise 3...")

advanced/lesson3/list.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Milk
2+
Grapes
3+
Peanut butter
4+
Sugar
5+
Flour

advanced/lesson3/mix.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apple
2+
orange
3+
pear
4+
avocado
5+
fig
6+
plum

advanced/lesson3/only_a.txt

Whitespace-only changes.

advanced/lesson3/read.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
is
2+
not
3+
food

advanced/lesson3/shopping.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
A program to read and write from a shopping list file.
3+
'''
4+
FILE = 'list.txt'
5+
6+
while True:
7+
print("Would you like to (1) view shopping list, (2) add an item, (3) remove an item, or (4) quit?")
8+
choice = input("Choice: ")
9+
10+
if choice == '1':
11+
# TODO Simply print all items in list.txt to the screen
12+
print("Items in the shopping list: ")
13+
14+
elif choice == '2':
15+
# Get an input item from the user and append to the shopping list
16+
add = input("What item to add? ")
17+
18+
elif choice == '3':
19+
# Get an item to remove from the user
20+
remove = input("What item to remove? ")
21+
22+
# TODO Read all items from the shopping list
23+
items = []
24+
25+
26+
# TODO Write all items back into the shopping list if they are not
27+
# the item to be removed
28+
29+
elif choice == '4':
30+
print("Goodbye!")
31+
break
32+
else:
33+
print("Unrecognized choice.")
34+
print()

advanced/lesson3/shopping_complete.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
A program to read and write from a shopping list file.
3+
'''
4+
FILE = 'list.txt'
5+
6+
while True:
7+
print("Would you like to (1) view shopping list, (2) add an item, (3) remove an item, or (4) quit?")
8+
choice = input("Choice: ")
9+
10+
if choice == '1':
11+
# TODO Simply print all items in list.txt to the screen
12+
print("Items in the shopping list: ")
13+
with open(FILE, 'r') as file:
14+
for line in file:
15+
print('-\t' + line)
16+
elif choice == '2':
17+
# Get an input item from the user and append to the shopping list
18+
add = input("What item to add? ")
19+
with open(FILE, 'a') as file:
20+
file.write(add + '\n')
21+
elif choice == '3':
22+
# Get an item to remove from the user
23+
remove = input("What item to remove? ")
24+
25+
# TODO Read all items from the shopping list
26+
items = []
27+
with open(FILE, 'r') as file:
28+
for line in file:
29+
items.append(line.strip())
30+
31+
32+
# TODO Write all items back into the shopping list if they are not
33+
# the item to be removed
34+
with open(FILE, 'w') as file:
35+
for item in items:
36+
if item != remove.strip():
37+
file.write(item + "\n")
38+
elif choice == '4':
39+
print("Goodbye!")
40+
break
41+
else:
42+
print("Unrecognized choice.")
43+
print()

beginner/lesson1/hello.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello, World!")

0 commit comments

Comments
 (0)