-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock_tracker.py
More file actions
67 lines (43 loc) · 1.63 KB
/
Copy pathstock_tracker.py
File metadata and controls
67 lines (43 loc) · 1.63 KB
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
def display_available_stocks(stock_prices):
print("Available Stocks:")
for stock, price in stock_prices.items():
print(f"{stock} : ${price}")
def get_valid_stock(stock_prices):
stock = input("Enter stock name: ").upper()
while stock not in stock_prices:
print("Invalid stock! Please choose from the available stocks.")
stock = input("Enter the stock name: ").upper()
return stock
def calculate_portfolio(stock, stock_prices, total):
quantity = int(input("Enter quantity: "))
investment = stock_prices[stock] * quantity
total += investment
print(f"\nInvestment value: ${investment}")
return total
def save_portfolio(total):
with open("portfolio.txt","w") as file:
file.write("========== Portfolio Summary ==========\n")
file.write(f"Total Invested Value: ${total}\n")
file.write("=======================================\n")
def print_summary(total):
print("\n========== Portfolio Summary ==========")
print(f"Total Invested Value: ${total}")
print("=======================================")
def main():
stock_prices = {
"AAPL": 180,
"TSLA": 250,
"MSFT": 400,
"GOOGL": 170,
"AMZN": 150
}
display_available_stocks(stock_prices)
number_of_stocks= int(input("Enter the different no. of stock you own: "))
total = 0
for _ in range(number_of_stocks):
stock = get_valid_stock(stock_prices)
total = calculate_portfolio(stock,stock_prices,total)
print_summary(total)
save_portfolio(total)
if __name__ == "__main__":
main()