position.py changed on this slide
1# position.py — one position of the ledger
2ticker = "NESN"
3raw = Not a whole number"4.5" # text, as read from a file
4shares = int(raw)int() refuses
5price = 102.3
6cash = 1000.0
7last_sale = None
8print(raw * 2, shares * 2, type(raw), type(shares))
9
10shares = shares + 6 # buy 6 more
11cash -= 6 * price
12value = shares * price
13weight = value / (value + cash)
14can_buy = int(cash // price)
15rest = cash % price
16in_2y = value * 1.05 ** 2
17
18valid = len(ticker) <= 5 and shares > 0
19alert = weight > 0.5 or not valid
20
21print(cash, cash == 386.2, abs(cash - 386.2) < 0.01)
22print(f"{ticker}: {shares} shares, {value:,.2f} CHF")
23print(f"cash {cash:.2f}, weight {weight:.2f}")
24print(can_buy, rest, round(in_2y, 2))
25print(valid, alert, last_sale)
$ python position.py
Traceback (most recent call last):
File "position.py", line 4, in <module>
shares = int(raw)
^^^^^^^^
ValueError: invalid literal for int() with base 10: '4.5'