-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhw-0.txt
131 lines (111 loc) · 3.3 KB
/
hw-0.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
1. Enter the type of the Python expression 3.14159 below. Remember that capitalization is important.
- Float
2. An if statement can have how many elif parts?
- Unlimited, i.e., 0 or more
3. Consider the following Python code snippet:
def clock_helper(total_seconds):
"""
Helper function for a clock
"""
seconds_in_minute = total_seconds % 60
print (clock_helper(90))
- None
4. In Python, what character always appears at the end of the line before the start of an indented block of code?
- :
5. Which of the following expressions returns the last character in the non-empty string my_string?
- my_string[len(my_string) - 1]
- my_string[-1]
6. What is the primary difference between a list and a tuple?
- Lists are mutable. Tuples are immutable.
7. Consider the following snippet of Python code. What is the value of val2[1] after executing this code?
val1 = [1, 2, 3]
val2 = val1[1:]
val1[2] = 4
#print val2[1]
- 3
8. Which of the following Python expressions is a valid key in a Python dictionary?
- False, "", 0
9.
def appendsums(lst):
sum = 0
for i in range(25):
sum = lst[len(lst) -1] + lst[len(lst) -2] + lst[len(lst) -3]
#print sum
lst.append(sum)
return lst
sum_three = [0, 1, 2]
appendsums(sum_three)
#print sum_three[10]
print sum_three[20]
- 101902
10.
class BankAccount:
balance = 0
fee = 0
def __init__(self, initial_balance):
"""Creates an account with the given balance."""
self.balance = initial_balance
def deposit(self, amount):
"""Deposits the amount into the account."""
self.balance = self.balance + amount
def withdraw(self, amount):
"""
Withdraws the amount from the account. Each withdrawal resulting in a
negative balance also deducts a penalty fee of 5 dollars from the balance.
"""
if self.balance - amount < 0:
self.balance = self.balance - amount -5
self.fee = self.fee + 5
else:
self.balance = self.balance - amount
def get_balance(self):
"""Returns the current balance in the account."""
return self.balance
def get_fees(self):
"""Returns the total fees ever deducted from the account."""
return self.fee
my_account = BankAccount(10)
my_account.withdraw(5)
my_account.deposit(10)
my_account.withdraw(5)
my_account.withdraw(15)
my_account.deposit(20)
my_account.withdraw(5)
my_account.deposit(10)
my_account.deposit(20)
my_account.withdraw(15)
my_account.deposit(30)
my_account.withdraw(10)
my_account.withdraw(15)
my_account.deposit(10)
my_account.withdraw(50)
my_account.deposit(30)
my_account.withdraw(15)
my_account.deposit(10)
my_account.withdraw(5)
my_account.deposit(20)
my_account.withdraw(15)
my_account.deposit(10)
my_account.deposit(30)
my_account.withdraw(25)
my_account.withdraw(5)
my_account.deposit(10)
my_account.withdraw(15)
my_account.deposit(10)
my_account.withdraw(10)
my_account.withdraw(15)
my_account.deposit(10)
my_account.deposit(30)
my_account.withdraw(25)
my_account.withdraw(10)
my_account.deposit(20)
my_account.deposit(10)
my_account.withdraw(5)
my_account.withdraw(15)
my_account.deposit(10)
my_account.withdraw(5)
my_account.withdraw(15)
my_account.deposit(10)
my_account.withdraw(5)
print (my_account.get_balance(), my_account.get_fees())
- (-60, 75)