+1 (315) 557-6473 

Stock Prices Homework Solution using Python


This assignment is to write a program in Python to allow a user to (A)dd, (D)elete, (L)ist and (Q)uit a program you write to maintain and update a {dictionary} of stock prices. When the program starts, it should display "(A)dd, (D)elete, (L)ist, (Q)uit" and allow you to A, D or L until you hit Q--at which point the program should exit.


import stockquotes stocks = dict() def add(): print("What stock wanted to add? ") symbol = input().upper() #Check to see if the stock exists if symbol in stocks: #If it does, then update the price stocks[symbol] = stockquotes.Stock(symbol).current_price else: #If it does not, then add the SYMbol (upper case) to the {dict} along with the current_price stocks[symbol] = stockquotes.Stock(symbol).current_price def delete(): print("What stock wanted to delete?") symbol = input().upper() #First check to see if the stock exists if symbol in stocks: #If it does, delete it (using del or .pop -- your choice) stocks.pop(symbol) else: #If it does not, inform the user that the symbol was not in the {dict} print("The provided symbol was not found in the stock list") def listing(): #Iterate over the list and print(SYMbol, price) #Make sure your list is sorted for i in sorted (stocks) : print ((i, stocks[i]), end =" ") print("\n") def menu(): print("(A)dd, (D)elete, (L)ist, (Q)uit") choice = input().lower() if choice == 'a': #(A)dding a stock to the list add() elif choice == 'd': #(D)eleting a stock delete() elif choice == 'l': #(L)isting the stocks listing() elif choice == 'q': #(Q)uit and exit the program. return else: print("Invalid request") #Recursively run the menu menu() menu()