# a. Sequential for loops. for i in range(0, 3): print('*') for i in range(0, 2): print('*') # Ans: Loops are sequential, so add: # 3 + 2 = 5. # b. Nested for loops. for i in range(0, 3): for j in range(0, 2): print('*') # Ans: Loops are nested so multiply: # 3 * 2 = 6 # c. Watch out for the indentation. for i in range(0, 9): print('*') print('*') print('*') # Ans: 9 * 2 + 1 = 19
for i in range(0, 200): print("*") for j in range(0, 300): print("*") for k in range(0, 500): print("*")a. 200 b. 300 c. 500 *d. 1,000
for i in range(0, 200): for j in range(0, 300): for k in range(0, 500): print("*")a. 500 b. 1,000 c. 300,000 *d. 60,000,000
for i in range(0, 200): for j in range(0, 300): print("*") for i in range(0, 500): print("*") print("*") print("*")a. 1,000 b. 1,003 *c. 61,001 d. 30,000,000
a = A( ) print(a)which dunder method is used to represent a as a string?
a = A( ) print([a])which dunder method is used to represent a as a string within the list?
+-----------------------------------------------------+ | BankAccount | +-----------------------------------------------------+ | + acct_number : int | | + acct_type : str | | + name : str | | + email : str | | + balance : float | +-----------------------------------------------------+ | - __init__(self : BankAccount, acct_number : int, | | acct_type : str, name : str, email : str) | | + deposit(self : BankAccount, amt : double) : bool | | + withdraw(self : BankAccount, amt : double) : bool | | - __str__(self : BankAccount) : str | +-----------------------------------------------------+
# Module: person.py class Person: def __init__(self, the_name, the_gender, the_age): self.name = the_name self.gender = the_gender self.age = the_age def have_birthday(self): self.age += 1 def __str__(self): return f"{self.name};{self.gender};{self.age}" def __repr__(self): return str(self) #---------------------------------------------------- # Module: test3.py from person import Person # Initialize list of Person objects persons = [ ] fin = open("persons.txt", "r") print(fin) # Throw away first line fin.readline( ) for line in fin: fields = line.split(",") # strip eliminates leading or trailing white # space name = fields[0].strip( ) gender = fields[1].strip( ) age = int(fields[2].strip( )) p = Person(name, gender, age) persons.append(p) fin.close( ) print(persons) #------------------------------------------------------To read the kids.txt file, replace the line
fin = open("persons.txt", "r")by
fin = open("persons.txt", "r")Also replace the line
fields = line.split(",")by
fields = line.split(" ")
a = ['dog', 'gerbil', 'cat', 'rabbit', 'mouse'] b = [28, 15, 93, 31, 11, 10] Ans: a.sort( ) print(a) b.sort( ) print(b)
TypeError: '<' not supported between instances of 'Person' and 'Person'
def __lt__(self, other): return self.age < other.age
# Source code file: test1.py from person import Person p1 = Person("Alice", "F", 11) p2 = Person("Bob", "M", 13) print(p1 < p2, p2 < p1) # Source code file: test2.py from person import Person import unittest class MyUnitTestClass(unittest.TestCase): def test_1(self): self.assertEqual(p1 < p2, True) self.assertEqual(p2 < p1, False) if __name__ == '__main__': unittest.main( )