-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Required concepts: https://www.w3schools.com/python/python_intro.asp OOPS & list comprehension | ||
|
||
print('Question: Make a list of objects with different properties') | ||
|
||
class Bird: | ||
def __init__(self,name,color): | ||
# this constructor creates a Bird with name | ||
self.name = name | ||
self.color = color | ||
|
||
all_names = ['parrot', 'ostrich'] | ||
all_colors = ['green', 'blue'] | ||
# n: no. of birds | ||
n = len(all_names) | ||
|
||
all_birds = [Bird(all_names[i],all_colors[i]) for i in range(n)] | ||
|
||
for bird in all_birds: | ||
print(bird.name, bird.color) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,15 @@ | ||
# Required concepts: https://www.w3schools.com/python/python_file_handling.asp file handling | ||
import os | ||
# Required concepts: https://www.w3schools.com/python/python_intro.asp OOPS & list comprehension | ||
|
||
print('Question: Find money owed to friends') | ||
print('Question: Make a list of objects with different names') | ||
|
||
class Bird: | ||
def __init__(self,name): | ||
# this constructor creates a Bird with name | ||
self.name = name | ||
|
||
def update_file(name, amount): | ||
path = 'tmp/' + name + '.txt' | ||
# get current amount | ||
reader = open(path, 'r') | ||
current_amount = int('0'+reader.read()) | ||
# write the new amount | ||
writer = open(path, 'w') | ||
writer.write(str(current_amount + amount)) | ||
all_names = ['parrot', 'ostrich'] | ||
|
||
all_birds = [Bird(name) for name in all_names] | ||
|
||
names = ['raj', 'riya', 'raj'] | ||
amounts = [1000, 200, 300] | ||
|
||
# # first make empty files for all :) | ||
for name in names: | ||
open('tmp/'+name+'.txt','w') | ||
|
||
for name, amount in zip(names, amounts): | ||
update_file(name, amount) | ||
|
||
print('Made the files with total debts') | ||
for bird in all_birds: | ||
print(bird.name) |