SRC 9
SRC 9
py
1 # UnboundLocalError
2
3 balance = 0
4
5
6 def main():
7 print("Balance:", balance)
8 deposit(100)
9 withdraw(50)
10 print("Balance:", balance)
11
12
13 def deposit(n):
14 balance += n
15
16
17 def withdraw(n):
18 balance -= n
19
20
21 if __name__ == "__main__":
22 main()
bank2.py
1 # Uses global
2
3 balance = 0
4
5
6 def main():
7 print("Balance:", balance)
8 deposit(100)
9 withdraw(50)
10 print("Balance:", balance)
11
12
13 def deposit(n):
14 global balance
15 balance += n
16
17
18 def withdraw(n):
19 global balance
20 balance -= n
21
22
23 if __name__ == "__main__":
24 main()
bank3.py
1 # Uses class
2
3
4 class Account:
5 def __init__(self):
6 self._balance = 0
7
8 @property
9 def balance(self):
10 return self._balance
11
12 def deposit(self, n):
13 self._balance += n
14
15 def withdraw(self, n):
16 self._balance -= n
17
18
19 def main():
20 account = Account()
21 print("Balance:", account.balance)
22 account.deposit(100)
23 account.withdraw(50)
24 print("Balance:", account.balance)
25
26
27 if __name__ == "__main__":
28 main()
meows0.py
1 # Demonstrates a constant
2
3 MEOWS = 3
4
5 for _ in range(MEOWS):
6 print("meow")
meows1.py
1 # Demonstrates TypeError
2
3
4 def meow(n):
5 for _ in range(n):
6 print("meow")
7
8
9 number = input("Number: ")
10 meow(number)
meows3.py
1 # Success
2
3
4 def meow(n: int):
5 for _ in range(n):
6 print("meow")
7
8
9 number: int = int(input("Number: "))
10 meow(number)
meows6.py
1 # Success
2
3
4 def meow(n: int) -> str:
5 return "meow\n" * n
6
7
8 number: int = int(input("Number: "))
9 meows: str = meow(number)
10 print(meows, end="")
meows9.py
1 # Unpacks a list
2
3 first, _ = input("What's your name? ").split(" ")
4 print(f"hello, {first}")
unpack1.py
1 # Unpacks a list
2
3
4 def total(galleons, sickles, knuts):
5 return (galleons * 17 + sickles) * 29 + knuts
6
7
8 coins = [100, 50, 25]
9
10 print(total(*coins), "Knuts")
unpack4.py
1 # Unpacks a dict
2
3
4 def total(galleons, sickles, knuts):
5 return (galleons * 17 + sickles) * 29 + knuts
6
7
8 coins = {"galleons": 100, "sickles": 50, "knuts": 25}
9
10 print(total(**coins), "Knuts")
unpack7.py
1 # Passes a list
2
3
4 def main():
5 yell(["This", "is", "CS50"])
6
7
8 def yell(words):
9 uppercased = []
10 for word in words:
11 uppercased.append(word.upper())
12 print(*uppercased)
13
14
15 if __name__ == "__main__":
16 main()
yell2.py
1 # Uses map
2
3
4 def main():
5 yell("This", "is", "CS50")
6
7
8 def yell(*words):
9 uppercased = map(str.upper, words)
10 print(*uppercased)
11
12
13 if __name__ == "__main__":
14 main()
yell4.py
1 # Prints n sheep
2
3 n = int(input("What's n? "))
4 for i in range(n):
5 print("🐑" * i)
sleep1.py
1 # Adds main
2
3
4 def main():
5 n = int(input("What's n? "))
6 for i in range(n):
7 print("🐑" * i)
8
9
10 if __name__ == "__main__":
11 main()
sleep2.py
1 # Uses yield
2
3
4 def main():
5 n = int(input("What's n? "))
6 for s in sheep(n):
7 print(s)
8
9
10 def sheep(n):
11 for i in range(n):
12 yield "🐑" * i
13
14
15 if __name__ == "__main__":
16 main()
say.py
1 import cowsay
2 import pyttsx3
3
4 engine = pyttsx3.init()
5 this = input("What's this? ")
6 cowsay.cow(this)
7 engine.say(this)
8 engine.runAndWait()