Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 18f263d

Browse filesBrowse files
authored
Merge pull request #25 from codedex-io/solutions-python-ch7
Add solution files for remainder of Ch. 7
2 parents 238c038 + 2c44ffc commit 18f263d
Copy full SHA for 18f263d

File tree

Expand file treeCollapse file tree

3 files changed

+94
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+94
-0
lines changed
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Favorite Cities 🏙️
2+
# Codédex
3+
4+
class City:
5+
def __init__(self, name, country, population, landmarks):
6+
self.name = name
7+
self.country = country
8+
self.population = population
9+
self.landmarks = landmarks
10+
11+
12+
nyc = City("New York City", "USA", 8468000, ["Statue of Liberty", "Brooklyn Bridge", "Apollo Theatre"])
13+
14+
shanghai = City("Shanghai", "China", 26320000, ["The Bund", "Jin Mao Tower", "Tianzifang"])
15+
16+
print(vars(nyc))
17+
print(vars(shanghai))
18+

‎7-classes-objects/37_bank_accounts.py

Copy file name to clipboard
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Bank Accounts 🏦
2+
# Codédex
3+
4+
class BankAccount:
5+
def __init__(self, first_name, last_name, account_id, account_type, pin, balance):
6+
self.first_name = first_name
7+
self.last_name = last_name
8+
self.account_id = account_id
9+
self.account_type = account_type
10+
self.pin = pin
11+
self.balance = balance
12+
13+
def deposit(self, amount):
14+
self.balance = self.balance + amount
15+
return self.balance
16+
17+
def withdraw(self, amount):
18+
self.balance = self.balance - amount
19+
return self.balance
20+
21+
def display_balance(self):
22+
print(f"${self.balance}")
23+
24+
checking_account = BankAccount("Jane", "Doe", 13243546, "checking", 0000, 250.00)
25+
26+
checking_account.deposit(100)
27+
28+
checking_account.display_balance()
29+
30+
checking_account.withdraw(50)
31+
32+
checking_account.display_balance()

‎7-classes-objects/38_pokedex.py

Copy file name to clipboard
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Pokédex 📟
2+
# Codédex
3+
4+
# Class definition
5+
class Pokemon:
6+
def __init__(self, name, type, description, level, region, is_caught):
7+
self.name = name
8+
self.type = type
9+
self.description = description
10+
self.level = level
11+
self.region = region
12+
self.is_caught = is_caught
13+
14+
# Instance method
15+
def speak(self):
16+
print(self.name + ' made a sound!')
17+
18+
# Instance method
19+
def display_details(self):
20+
print('Name: ' + self.name)
21+
22+
if len(self.type) == 1:
23+
print('Type: ' + self.type[0])
24+
else:
25+
print('Type: ' + self.type[0] + '/' + self.type[1])
26+
27+
print('Lv. ' + self.level)
28+
print('Region: ' + self.region)
29+
print('Description: ' + self.description)
30+
31+
if self.is_caught:
32+
print(self.name + ' has already been caught!')
33+
else:
34+
print(self.name + ' hasn\'t been caught yet.')
35+
36+
# Pokémon objects
37+
pikachu = Pokemon("Pikachu", ["Electric"], " It has small electric sacs on both its cheeks. If threatened, it looses electric charges from the sacs.", 25, "Kanto", True)
38+
39+
charizard = Pokemon("Charizard", ["Fire", "Flying"], " It spits fire that is hot enough to melt boulders. It may cause forest fires by blowing flames.", 36, "Kanto", False)
40+
41+
gyarados = Pokemon("Gyarados", ["Water", "Flying"], "It has an extremely aggressive nature. The HYPER BEAM it shoots from its mouth totally incinerates all targets.", 57, "Kanto", False)
42+
43+
44+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.