Question

In: Computer Science

#Problem 2 Write function write_stock that asks the user to input a company name, its associated...

#Problem 2

Write function write_stock that asks the user to input a company name, its associated ticker symbol (eg. Exxon, XOM), and the price per share of the stock. These are then written to file stock.txt one item per line. The user will type in ‘quit’ to stop.

Write function get_share_value that accepts a company name and the number of shares. The function will search file stock.txt and return a list of two values; the ticker symbol and the total value of the shares. This function must use an exception handler to insure the file is valid, that the price can be converted to a number, and catch any other exception that might be raised.

Write a program that calls write_stock and get_share_value. The program will ask the user for the company name and number of shares for read_stock and print the answer.

Sample output might look as follows.

Enter company name; enter quit to stop: Amazon

Enter ticker symbol: amzn

Enter price: 1000

Enter company name; enter quit to stop: Apple

Enter ticker symbol: aapl

Enter price: 125

Enter company name; enter quit to stop: Microsoft

Enter ticker symbol: msft

Enter price: 250

Enter company name; enter quit to stop: quit

What stock did you buy?: Apple

How many shares?: 10

Your total value for aapl is $1250.00

PYTHON CODE

Solutions

Expert Solution

Below is the python solution with output screenshot

Code :

def write_stock():
    f = open("stock.txt", 'a')
    while(True):
        company = input("Enter company name; enter quit to stop: ")
        if(company == 'quit' or company == 'QUIT'):
            break
        else:
            symbol = input("Enter ticker symbol: ")
            price = input("Enter price: ")
            f.write(company+'\n')
            f.write(symbol+'\n')
            f.write(price+'\n')


def get_share_value ():
    company = input("What stock did you buy?: ")
    shares = input("How many shares?: ")
    try:
        f = open('stock.txt', 'r')
        while True:         
            # Get next line from file 
            line = f.readline() 
            line = line.rstrip()
            # if line is empty end of file is reached 
            if not line: 
                break
            if(line == company):
                symbol = f.readline()
                symbol = symbol.rstrip()
                price = f.readline()
                price = price.rstrip()
                try:
                    price = int(price)
                    shares = int(shares)
                    total_price = price*shares
                except:
                    print("Error: invalid price value")
                return [symbol, total_price]
    except:
        print("Some Error Occured")
        exit()


if __name__ == "__main__":
    write_stock()
    data = get_share_value()
    print('Your total value for', data[0], 'is $', round(data[1],2))

Output :

Note : for better understanding please refer to the code screenshot below


Related Solutions

Write a program that asks the user for a Fahrenheit temperature and calls a function name...
Write a program that asks the user for a Fahrenheit temperature and calls a function name Celsius that returns the temperature in Celsius of the Fahrenheit temperature sent to it. Your function will look similar to this: double celsius(double f) { double c; : : return c; }
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a program, ArrayRange, that asks the user to input integers and that displays the difference...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference between the largest and the smallest. You should ask the user the number of integers s/he would like to enter (this will allow you to set the length of the array). Allow the user to input all the numbers and then store them in the array. Search the array for the largest number, then search for the smallest, and finally compute the calculation. Display...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
Problem 2 (Save and Get Info) : Write a program that asks for the user's name,...
Problem 2 (Save and Get Info) : Write a program that asks for the user's name, phone number, and address. The program then saves all information in a data file (each information in one line) named list.txt. Finally, the program reads the information from the file and displays it on the screen  in the following format: Name: User's Name   Phone Number: User's Phone Number   Address: User's Street Address User's City, State, and Zip Code
C++ Write a program that asks a teacher to input a student’s first name, last name,...
C++ Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt” last_name first_name average A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted.  The program should then read the information in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT