{}
BLACK NOVEMBER
Get 66% off PRO
Are you struggling to build your coding confidence or land your first job?
Fast-track to your first pay-check.
Start PRO
Start PRO
BLACK NOVEMBER
Get 66% off PRO
Are you struggling to build your coding confidence or land your first job?
Fast-track to your first pay-check.
Start PRO
Start PRO
run-icon
main.py
# Stock Portfolio Tracker # Step 1: Hardcoded stock prices stock_prices = { "AAPL": 180, "TSLA": 250, "GOOGL": 140, "MSFT": 320, "AMZN": 135 } portfolio = {} total_investment = 0 print("šŸ“Š Welcome to Stock Portfolio Tracker šŸ“Š") print("Available stocks:", ", ".join(stock_prices.keys())) print("Enter 'done' when you are finished adding stocks.\n") # Step 2: Get user input for stocks while True: stock_name = input("Enter stock name (or 'done' to finish): ").upper() if stock_name == "DONE": break if stock_name not in stock_prices: print("āš ļø Stock not found! Please enter a valid stock name.") continue try: quantity = int(input(f"Enter quantity for {stock_name}: ")) except ValueError: print("āš ļø Please enter a valid number.") continue portfolio[stock_name] = portfolio.get(stock_name, 0) + quantity # Step 3: Calculate total investment print("\nšŸ“Œ Your Portfolio Summary:") for stock, qty in portfolio.items(): price = stock_prices[stock] value = qty * price total_investment += value print(f"{stock}: {qty} Ɨ ${price} = ${value}") print(f"\nšŸ’° Total Investment: ${total_investment}") # Step 4: Optional - Save to file save = input("\nDo you want to save this portfolio? (y/n): ").lower() if save == "y": file_name = "portfolio.csv" with open(file_name, "w") as f: f.write("Stock,Quantity,Price,Value\n") for stock, qty in portfolio.items(): f.write(f"{stock},{qty},{stock_prices[stock]},{qty * stock_prices[stock]}\n") f.write(f"Total Investment,,,{total_investment}\n") print(f"āœ… Portfolio saved to {file_name}")
Output