Added solutions documents for ease of reference
This commit is contained in:
parent
3a58770176
commit
bf85d67430
12
pcc-solutions/.gitignore
vendored
Normal file
12
pcc-solutions/.gitignore
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
.DS_Store
|
||||||
|
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
*_env/
|
||||||
|
.venv*/
|
||||||
|
|
||||||
|
online_resources_site/site/
|
||||||
|
|
||||||
|
*.sqlite3
|
||||||
|
|
||||||
13
pcc-solutions/README.md
Normal file
13
pcc-solutions/README.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
Python Crash Course - Third Edition
|
||||||
|
===
|
||||||
|
|
||||||
|
A Hands-On, Project-Based Introduction to Programming
|
||||||
|
---
|
||||||
|
|
||||||
|
This is a collection of resources for [Python Crash Course, Third Edition](https://nostarch.com/python-crash-course-3rd-edition), an introductory programming book from [No Starch Press](https://nostarch.com) by Eric Matthes. Click here for a [much cleaner version](https://ehmatthes.github.io/pcc_3e/) of these online resources.
|
||||||
|
|
||||||
|
If you have any questions about Python Crash Course, feel free to get in touch:
|
||||||
|
|
||||||
|
Email: ehmatthes@gmail.com
|
||||||
|
|
||||||
|
Twitter: [@ehmatthes](http://twitter.com/ehmatthes/)
|
||||||
1
pcc-solutions/chapter_01/hello_world.py
Normal file
1
pcc-solutions/chapter_01/hello_world.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
print("Hello Python world!")
|
||||||
2
pcc-solutions/chapter_02/apostrophe.py
Normal file
2
pcc-solutions/chapter_02/apostrophe.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
message = "One of Python's strengths is its diverse community."
|
||||||
|
print(message)
|
||||||
2
pcc-solutions/chapter_02/comment.py
Normal file
2
pcc-solutions/chapter_02/comment.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# Say hello to everyone.
|
||||||
|
print("Hello Python people!")
|
||||||
4
pcc-solutions/chapter_02/full_name.py
Normal file
4
pcc-solutions/chapter_02/full_name.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
first_name = "ada"
|
||||||
|
last_name = "lovelace"
|
||||||
|
full_name = f"{first_name} {last_name}"
|
||||||
|
print(full_name)
|
||||||
4
pcc-solutions/chapter_02/full_name_2.py
Normal file
4
pcc-solutions/chapter_02/full_name_2.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
first_name = "ada"
|
||||||
|
last_name = "lovelace"
|
||||||
|
full_name = f"{first_name} {last_name}"
|
||||||
|
print(f"Hello, {full_name.title()}!")
|
||||||
5
pcc-solutions/chapter_02/full_name_3.py
Normal file
5
pcc-solutions/chapter_02/full_name_3.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
first_name = "ada"
|
||||||
|
last_name = "lovelace"
|
||||||
|
full_name = f"{first_name} {last_name}"
|
||||||
|
message = f"Hello, {full_name.title()}!"
|
||||||
|
print(message)
|
||||||
2
pcc-solutions/chapter_02/hello_world.py
Normal file
2
pcc-solutions/chapter_02/hello_world.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
message = "Hello Python world!"
|
||||||
|
print(message)
|
||||||
2
pcc-solutions/chapter_02/hello_world_variables.py
Normal file
2
pcc-solutions/chapter_02/hello_world_variables.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
message = "Hello Python world!"
|
||||||
|
print(message)
|
||||||
5
pcc-solutions/chapter_02/hello_world_variables_2.py
Normal file
5
pcc-solutions/chapter_02/hello_world_variables_2.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
message = "Hello Python world!"
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
message = "Hello Python Crash Course world!"
|
||||||
|
print(message)
|
||||||
2
pcc-solutions/chapter_02/name.py
Normal file
2
pcc-solutions/chapter_02/name.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
name = "ada lovelace"
|
||||||
|
print(name.title())
|
||||||
3
pcc-solutions/chapter_02/name_2.py
Normal file
3
pcc-solutions/chapter_02/name_2.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
name = "Ada Lovelace"
|
||||||
|
print(name.upper())
|
||||||
|
print(name.lower())
|
||||||
4
pcc-solutions/chapter_03/bicycles.py
Normal file
4
pcc-solutions/chapter_03/bicycles.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
|
||||||
|
message = f"My first bicycle was a {bicycles[0].title()}."
|
||||||
|
|
||||||
|
print(message)
|
||||||
5
pcc-solutions/chapter_03/cars.py
Normal file
5
pcc-solutions/chapter_03/cars.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
cars = ['bmw', 'audi', 'toyota', 'subaru']
|
||||||
|
print(cars)
|
||||||
|
|
||||||
|
cars.reverse()
|
||||||
|
print(cars)
|
||||||
7
pcc-solutions/chapter_03/motorcycles.py
Normal file
7
pcc-solutions/chapter_03/motorcycles.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
|
||||||
|
print(motorcycles)
|
||||||
|
|
||||||
|
too_expensive = 'ducati'
|
||||||
|
motorcycles.remove(too_expensive)
|
||||||
|
print(motorcycles)
|
||||||
|
print(f"\nA {too_expensive.title()} is too expensive for me.")
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
|
||||||
|
print(bicycles)
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
|
||||||
|
print(bicycles[0])
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
|
||||||
|
print(bicycles[0].title())
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
|
||||||
|
print(bicycles[1])
|
||||||
|
print(bicycles[3])
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
|
||||||
|
print(bicycles[-1])
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
|
||||||
|
message = f"My first bicycle was a {bicycles[0].title()}."
|
||||||
|
|
||||||
|
print(message)
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
cars = ['bmw', 'audi', 'toyota', 'subaru']
|
||||||
|
cars.sort()
|
||||||
|
print(cars)
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
cars = ['bmw', 'audi', 'toyota', 'subaru']
|
||||||
|
cars.sort(reverse=True)
|
||||||
|
print(cars)
|
||||||
10
pcc-solutions/chapter_03/partial_programs/cars_2_sorted.py
Normal file
10
pcc-solutions/chapter_03/partial_programs/cars_2_sorted.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
cars = ['bmw', 'audi', 'toyota', 'subaru']
|
||||||
|
|
||||||
|
print("Here is the original list:")
|
||||||
|
print(cars)
|
||||||
|
|
||||||
|
print("\nHere is the sorted list:")
|
||||||
|
print(sorted(cars))
|
||||||
|
|
||||||
|
print("\nHere is the original list again:")
|
||||||
|
print(cars)
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
cars = ['bmw', 'audi', 'toyota', 'subaru']
|
||||||
|
print(cars)
|
||||||
|
|
||||||
|
cars.reverse()
|
||||||
|
print(cars)
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki']
|
||||||
|
print(motorcycles)
|
||||||
|
|
||||||
|
motorcycles[0] = 'ducati'
|
||||||
|
print(motorcycles)
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
|
||||||
|
print(motorcycles)
|
||||||
|
|
||||||
|
too_expensive = 'ducati'
|
||||||
|
motorcycles.remove(too_expensive)
|
||||||
|
print(motorcycles)
|
||||||
|
print(f"\nA {too_expensive.title()} is too expensive for me.")
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki']
|
||||||
|
print(motorcycles[3])
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki']
|
||||||
|
print(motorcycles)
|
||||||
|
|
||||||
|
motorcycles.append('ducati')
|
||||||
|
print(motorcycles)
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
motorcycles = []
|
||||||
|
|
||||||
|
motorcycles.append('honda')
|
||||||
|
motorcycles.append('yamaha')
|
||||||
|
motorcycles.append('suzuki')
|
||||||
|
|
||||||
|
print(motorcycles)
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki']
|
||||||
|
|
||||||
|
motorcycles.insert(0, 'ducati')
|
||||||
|
print(motorcycles)
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki']
|
||||||
|
print(motorcycles)
|
||||||
|
|
||||||
|
del motorcycles[0]
|
||||||
|
print(motorcycles)
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki']
|
||||||
|
print(motorcycles)
|
||||||
|
|
||||||
|
del motorcycles[1]
|
||||||
|
print(motorcycles)
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki']
|
||||||
|
print(motorcycles)
|
||||||
|
|
||||||
|
popped_motorcycle = motorcycles.pop()
|
||||||
|
print(motorcycles)
|
||||||
|
print(popped_motorcycle)
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki']
|
||||||
|
|
||||||
|
last_owned = motorcycles.pop()
|
||||||
|
print(f"The last motorcycle I owned was a {last_owned.title()}.")
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki']
|
||||||
|
|
||||||
|
first_owned = motorcycles.pop(0)
|
||||||
|
print(f"The first motorcycle I owned was a {first_owned.title()}.")
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
|
||||||
|
print(motorcycles)
|
||||||
|
|
||||||
|
motorcycles.remove('ducati')
|
||||||
|
print(motorcycles)
|
||||||
9
pcc-solutions/chapter_04/dimensions.py
Normal file
9
pcc-solutions/chapter_04/dimensions.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
dimensions = (200, 50)
|
||||||
|
print("Original dimensions:")
|
||||||
|
for dimension in dimensions:
|
||||||
|
print(dimension)
|
||||||
|
|
||||||
|
dimensions = (400, 100)
|
||||||
|
print("\nModified dimensions:")
|
||||||
|
for dimension in dimensions:
|
||||||
|
print(dimension)
|
||||||
2
pcc-solutions/chapter_04/even_numbers.py
Normal file
2
pcc-solutions/chapter_04/even_numbers.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
even_numbers = list(range(2, 11, 2))
|
||||||
|
print(even_numbers)
|
||||||
2
pcc-solutions/chapter_04/first_numbers.py
Normal file
2
pcc-solutions/chapter_04/first_numbers.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
numbers = list(range(1, 6))
|
||||||
|
print(numbers)
|
||||||
11
pcc-solutions/chapter_04/foods.py
Normal file
11
pcc-solutions/chapter_04/foods.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
my_foods = ['pizza', 'falafel', 'carrot cake']
|
||||||
|
friend_foods = my_foods[:]
|
||||||
|
|
||||||
|
my_foods.append('cannoli')
|
||||||
|
friend_foods.append('ice cream')
|
||||||
|
|
||||||
|
print("My favorite foods are:")
|
||||||
|
print(my_foods)
|
||||||
|
|
||||||
|
print("\nMy friend's favorite foods are:")
|
||||||
|
print(friend_foods)
|
||||||
6
pcc-solutions/chapter_04/magicians.py
Normal file
6
pcc-solutions/chapter_04/magicians.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
magicians = ['alice', 'david', 'carolina']
|
||||||
|
for magician in magicians:
|
||||||
|
print(f"{magician.title()}, that was a great trick!")
|
||||||
|
print(f"I can't wait to see your next trick, {magician.title()}.\n")
|
||||||
|
|
||||||
|
print("Thank you, everyone. That was a great magic show!")
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
dimensions = (200, 50)
|
||||||
|
print(dimensions[0])
|
||||||
|
print(dimensions[1])
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
dimensions = (200, 50)
|
||||||
|
dimensions[0] = 250
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
dimensions = (200, 50)
|
||||||
|
for dimension in dimensions:
|
||||||
|
print(dimension)
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
dimensions = (200, 50)
|
||||||
|
print("Original dimensions:")
|
||||||
|
for dimension in dimensions:
|
||||||
|
print(dimension)
|
||||||
|
|
||||||
|
dimensions = (400, 100)
|
||||||
|
print("\nModified dimensions:")
|
||||||
|
for dimension in dimensions:
|
||||||
|
print(dimension)
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
even_numbers = list(range(2, 11, 2))
|
||||||
|
print(even_numbers)
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
for value in range(1, 5):
|
||||||
|
print(value)
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
for value in range(1, 6):
|
||||||
|
print(value)
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
numbers = list(range(1, 6))
|
||||||
|
print(numbers)
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
my_foods = ['pizza', 'falafel', 'carrot cake']
|
||||||
|
friend_foods = my_foods[:]
|
||||||
|
|
||||||
|
print("My favorite foods are:")
|
||||||
|
print(my_foods)
|
||||||
|
|
||||||
|
print("\nMy friend's favorite foods are:")
|
||||||
|
print(friend_foods)
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
my_foods = ['pizza', 'falafel', 'carrot cake']
|
||||||
|
friend_foods = my_foods[:]
|
||||||
|
|
||||||
|
my_foods.append('cannoli')
|
||||||
|
friend_foods.append('ice cream')
|
||||||
|
|
||||||
|
print("My favorite foods are:")
|
||||||
|
print(my_foods)
|
||||||
|
|
||||||
|
print("\nMy friend's favorite foods are:")
|
||||||
|
print(friend_foods)
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
my_foods = ['pizza', 'falafel', 'carrot cake']
|
||||||
|
|
||||||
|
# This doesn't work:
|
||||||
|
friend_foods = my_foods
|
||||||
|
|
||||||
|
my_foods.append('cannoli')
|
||||||
|
friend_foods.append('ice cream')
|
||||||
|
|
||||||
|
print("My favorite foods are:")
|
||||||
|
print(my_foods)
|
||||||
|
|
||||||
|
print("\nMy friend's favorite foods are:")
|
||||||
|
print(friend_foods)
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
message = "Hello Python world!"
|
||||||
|
print(message)
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
magicians = ['alice', 'david', 'carolina']
|
||||||
|
for magician in magicians:
|
||||||
|
print(magician)
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
magicians = ['alice', 'david', 'carolina']
|
||||||
|
for magician in magicians:
|
||||||
|
print(f"{magician.title()}, that was a great trick!")
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
magicians = ['alice', 'david', 'carolina']
|
||||||
|
for magician in magicians:
|
||||||
|
print(f"{magician.title()}, that was a great trick!")
|
||||||
|
print(f"I can't wait to see your next trick, {magician.title()}.\n")
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
magicians = ['alice', 'david', 'carolina']
|
||||||
|
for magician in magicians:
|
||||||
|
print(f"{magician.title()}, that was a great trick!")
|
||||||
|
print(f"I can't wait to see your next trick, {magician.title()}.\n")
|
||||||
|
|
||||||
|
print("Thank you, everyone. That was a great magic show!")
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
magicians = ['alice', 'david', 'carolina']
|
||||||
|
for magician in magicians:
|
||||||
|
print(magician)
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
magicians = ['alice', 'david', 'carolina']
|
||||||
|
for magician in magicians:
|
||||||
|
print(f"{magician.title()}, that was a great trick!")
|
||||||
|
print(f"I can't wait to see your next trick, {magician.title()}.\n")
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
magicians = ['alice', 'david', 'carolina']
|
||||||
|
for magician in magicians:
|
||||||
|
print(f"{magician.title()}, that was a great trick!")
|
||||||
|
print(f"I can't wait to see your next trick, {magician.title()}.\n")
|
||||||
|
|
||||||
|
print("Thank you everyone, that was a great magic show!")
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
magicians = ['alice', 'david', 'carolina']
|
||||||
|
for magician in magicians
|
||||||
|
print(magician)
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
players = ['charles', 'martina', 'michael', 'florence', 'eli']
|
||||||
|
print(players[0:3])
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
players = ['charles', 'martina', 'michael', 'florence', 'eli']
|
||||||
|
print(players[1:4])
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
players = ['charles', 'martina', 'michael', 'florence', 'eli']
|
||||||
|
print(players[:4])
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
players = ['charles', 'martina', 'michael', 'florence', 'eli']
|
||||||
|
print(players[2:])
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
players = ['charles', 'martina', 'michael', 'florence', 'eli']
|
||||||
|
print(players[-3:])
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
players = ['charles', 'martina', 'michael', 'florence', 'eli']
|
||||||
|
|
||||||
|
print("Here are the first three players on my team:")
|
||||||
|
for player in players[:3]:
|
||||||
|
print(player.title())
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
squares = []
|
||||||
|
for value in range(1, 11):
|
||||||
|
square = value ** 2
|
||||||
|
squares.append(square)
|
||||||
|
|
||||||
|
print(squares)
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
squares = []
|
||||||
|
for value in range(1, 11):
|
||||||
|
squares.append(value**2)
|
||||||
|
|
||||||
|
print(squares)
|
||||||
2
pcc-solutions/chapter_04/partial_programs/squares.py
Normal file
2
pcc-solutions/chapter_04/partial_programs/squares.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
squares = [value**2 for value in range(1, 11)]
|
||||||
|
print(squares)
|
||||||
5
pcc-solutions/chapter_04/players.py
Normal file
5
pcc-solutions/chapter_04/players.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
players = ['charles', 'martina', 'michael', 'florence', 'eli']
|
||||||
|
|
||||||
|
print("Here are the first three players on my team:")
|
||||||
|
for player in players[:3]:
|
||||||
|
print(player.title())
|
||||||
5
pcc-solutions/chapter_04/square_numbers.py
Normal file
5
pcc-solutions/chapter_04/square_numbers.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
squares = []
|
||||||
|
for value in range(1, 11):
|
||||||
|
squares.append(value**2)
|
||||||
|
|
||||||
|
print(squares)
|
||||||
2
pcc-solutions/chapter_04/squares.py
Normal file
2
pcc-solutions/chapter_04/squares.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
squares = [value**2 for value in range(1, 11)]
|
||||||
|
print(squares)
|
||||||
12
pcc-solutions/chapter_05/amusement_park.py
Normal file
12
pcc-solutions/chapter_05/amusement_park.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
age = 12
|
||||||
|
|
||||||
|
if age < 4:
|
||||||
|
price = 0
|
||||||
|
elif age < 18:
|
||||||
|
price = 25
|
||||||
|
elif age < 65:
|
||||||
|
price = 40
|
||||||
|
elif age >= 65:
|
||||||
|
price = 20
|
||||||
|
|
||||||
|
print(f"Your admission cost is ${price}.")
|
||||||
5
pcc-solutions/chapter_05/banned_users.py
Normal file
5
pcc-solutions/chapter_05/banned_users.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
banned_users = ['andrew', 'carolina', 'david']
|
||||||
|
user = 'marie'
|
||||||
|
|
||||||
|
if user not in banned_users:
|
||||||
|
print(f"{user.title()}, you can post a response if you wish.")
|
||||||
7
pcc-solutions/chapter_05/cars.py
Normal file
7
pcc-solutions/chapter_05/cars.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
cars = ['audi', 'bmw', 'subaru', 'toyota']
|
||||||
|
|
||||||
|
for car in cars:
|
||||||
|
if car == 'bmw':
|
||||||
|
print(car.upper())
|
||||||
|
else:
|
||||||
|
print(car.title())
|
||||||
4
pcc-solutions/chapter_05/magic_number.py
Normal file
4
pcc-solutions/chapter_05/magic_number.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
answer = 17
|
||||||
|
|
||||||
|
if answer != 42:
|
||||||
|
print("That is not the correct answer. Please try again!")
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
age = 12
|
||||||
|
if age < 4:
|
||||||
|
print("Your admission cost is $0.")
|
||||||
|
elif age < 18:
|
||||||
|
print("Your admission cost is $25.")
|
||||||
|
else:
|
||||||
|
print("Your admission cost is $40.")
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
age = 12
|
||||||
|
|
||||||
|
if age < 4:
|
||||||
|
price = 0
|
||||||
|
elif age < 18:
|
||||||
|
price = 25
|
||||||
|
else:
|
||||||
|
price = 40
|
||||||
|
|
||||||
|
print(f"Your admission cost is ${price}.")
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
age = 12
|
||||||
|
|
||||||
|
if age < 4:
|
||||||
|
price = 0
|
||||||
|
elif age < 18:
|
||||||
|
price = 25
|
||||||
|
elif age < 65:
|
||||||
|
price = 40
|
||||||
|
else:
|
||||||
|
price = 20
|
||||||
|
|
||||||
|
print(f"Your admission cost is ${price}.")
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
age = 12
|
||||||
|
|
||||||
|
if age < 4:
|
||||||
|
price = 0
|
||||||
|
elif age < 18:
|
||||||
|
price = 25
|
||||||
|
elif age < 65:
|
||||||
|
price = 40
|
||||||
|
elif age >= 65:
|
||||||
|
price = 20
|
||||||
|
|
||||||
|
print(f"Your admission cost is ${price}.")
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
banned_users = ['andrew', 'carolina', 'david']
|
||||||
|
user = 'marie'
|
||||||
|
|
||||||
|
if user not in banned_users:
|
||||||
|
print(f"{user.title()}, you can post a response if you wish.")
|
||||||
7
pcc-solutions/chapter_05/partial_programs/cars.py
Normal file
7
pcc-solutions/chapter_05/partial_programs/cars.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
cars = ['audi', 'bmw', 'subaru', 'toyota']
|
||||||
|
|
||||||
|
for car in cars:
|
||||||
|
if car == 'bmw':
|
||||||
|
print(car.upper())
|
||||||
|
else:
|
||||||
|
print(car.title())
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
answer = 17
|
||||||
|
|
||||||
|
if answer != 42:
|
||||||
|
print("That is not the correct answer. Please try again!")
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
requested_topping = 'mushrooms'
|
||||||
|
|
||||||
|
if requested_topping != 'anchovies':
|
||||||
|
print("Hold the anchovies!")
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
requested_toppings = ['mushrooms', 'extra cheese']
|
||||||
|
|
||||||
|
if 'mushrooms' in requested_toppings:
|
||||||
|
print("Adding mushrooms.")
|
||||||
|
if 'pepperoni' in requested_toppings:
|
||||||
|
print("Adding pepperoni.")
|
||||||
|
if 'extra cheese' in requested_toppings:
|
||||||
|
print("Adding extra cheese.")
|
||||||
|
|
||||||
|
print("\nFinished making your pizza!")
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
|
||||||
|
|
||||||
|
for requested_topping in requested_toppings:
|
||||||
|
print(f"Adding {requested_topping}.")
|
||||||
|
|
||||||
|
print("\nFinished making your pizza!")
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
|
||||||
|
|
||||||
|
for requested_topping in requested_toppings:
|
||||||
|
if requested_topping == 'green peppers':
|
||||||
|
print("Sorry, we are out of green peppers right now.")
|
||||||
|
else:
|
||||||
|
print(f"Adding {requested_topping}.")
|
||||||
|
|
||||||
|
print("\nFinished making your pizza!")
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
requested_toppings = []
|
||||||
|
|
||||||
|
if requested_toppings:
|
||||||
|
for requested_topping in requested_toppings:
|
||||||
|
print(f"Adding {requested_topping}.")
|
||||||
|
print("\nFinished making your pizza!")
|
||||||
|
else:
|
||||||
|
print("Are you sure you want a plain pizza?")
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
available_toppings = ['mushrooms', 'olives', 'green peppers',
|
||||||
|
'pepperoni', 'pineapple', 'extra cheese']
|
||||||
|
|
||||||
|
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
|
||||||
|
|
||||||
|
for requested_topping in requested_toppings:
|
||||||
|
if requested_topping in available_toppings:
|
||||||
|
print(f"Adding {requested_topping}.")
|
||||||
|
else:
|
||||||
|
print(f"Sorry, we don't have {requested_topping}.")
|
||||||
|
|
||||||
|
print("\nFinished making your pizza!")
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
age = 19
|
||||||
|
if age >= 18:
|
||||||
|
print("You are old enough to vote!")
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
age = 19
|
||||||
|
if age >= 18:
|
||||||
|
print("You are old enough to vote!")
|
||||||
|
print("Have you registered to vote yet?")
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
age = 17
|
||||||
|
if age >= 18:
|
||||||
|
print("You are old enough to vote!")
|
||||||
|
print("Have you registered to vote yet?")
|
||||||
|
else:
|
||||||
|
print("Sorry, you are too young to vote.")
|
||||||
|
print("Please register to vote as soon as you turn 18!")
|
||||||
12
pcc-solutions/chapter_05/toppings.py
Normal file
12
pcc-solutions/chapter_05/toppings.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
available_toppings = ['mushrooms', 'olives', 'green peppers',
|
||||||
|
'pepperoni', 'pineapple', 'extra cheese']
|
||||||
|
|
||||||
|
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
|
||||||
|
|
||||||
|
for requested_topping in requested_toppings:
|
||||||
|
if requested_topping in available_toppings:
|
||||||
|
print(f"Adding {requested_topping}.")
|
||||||
|
else:
|
||||||
|
print(f"Sorry, we don't have {requested_topping}.")
|
||||||
|
|
||||||
|
print("\nFinished making your pizza!")
|
||||||
7
pcc-solutions/chapter_05/voting.py
Normal file
7
pcc-solutions/chapter_05/voting.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
age = 17
|
||||||
|
if age >= 18:
|
||||||
|
print("You are old enough to vote!")
|
||||||
|
print("Have you registered to vote yet?")
|
||||||
|
else:
|
||||||
|
print("Sorry, you are too young to vote.")
|
||||||
|
print("Please register to vote as soon as you turn 18!")
|
||||||
5
pcc-solutions/chapter_06/alien.py
Normal file
5
pcc-solutions/chapter_06/alien.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
alien_0 = {'color': 'green', 'points': 5}
|
||||||
|
print(alien_0)
|
||||||
|
|
||||||
|
del alien_0['points']
|
||||||
|
print(alien_0)
|
||||||
4
pcc-solutions/chapter_06/alien_no_points.py
Normal file
4
pcc-solutions/chapter_06/alien_no_points.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
alien_0 = {'color': 'green', 'speed': 'slow'}
|
||||||
|
|
||||||
|
point_value = alien_0.get('points', 'No point value assigned.')
|
||||||
|
print(point_value)
|
||||||
21
pcc-solutions/chapter_06/aliens.py
Normal file
21
pcc-solutions/chapter_06/aliens.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Make an empty list for storing aliens.
|
||||||
|
aliens = []
|
||||||
|
|
||||||
|
# Make 30 green aliens.
|
||||||
|
for alien_number in range(30):
|
||||||
|
new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
|
||||||
|
aliens.append(new_alien)
|
||||||
|
|
||||||
|
for alien in aliens[:3]:
|
||||||
|
if alien['color'] == 'green':
|
||||||
|
alien['color'] = 'yellow'
|
||||||
|
alien['speed'] = 'medium'
|
||||||
|
alien['points'] = 10
|
||||||
|
|
||||||
|
# Show the first 5 aliens.
|
||||||
|
for alien in aliens[:5]:
|
||||||
|
print(alien)
|
||||||
|
print("...")
|
||||||
|
|
||||||
|
# Show how many aliens have been created.
|
||||||
|
print(f"Total number of aliens: {len(aliens)}")
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user