×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Stock Prices Homework Solution using Python

July 09, 2024
Dr. Nicholas Scott
Dr. Nicholas
🇯🇵 Japan
Python
Dr. Nicholas Scott, a distinguished Computer Science expert, holds a PhD from Tokyo University, Japan. With an impressive 8 years of experience, he has completed over 600 Python assignments, showcasing his unparalleled expertise and dedication to the field.
Tip of the day
Start with basic image processing concepts like color spaces and transformations. Practice simple OpenCV examples to build confidence before tackling complex tasks.
News
In 2024, the release of the 'AsyncIO Toolkit' has made asynchronous programming in Python more efficient. Similarly, 'React Quantum' introduces quantum computing concepts to JavaScript, expanding learning opportunities for students.

Assignment

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.

Solution:

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()

Similar Samples

Explore ProgrammingHomeworkHelp.com's diverse collection of programming samples. From Java and Python to C++ and SQL, our examples demonstrate proficiency across various languages and topics. Each sample showcases our commitment to delivering high-quality solutions tailored to academic needs. Dive into our samples to see how we can assist you in mastering programming challenges.