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 below in Python Get user name from keyboard using input() function (Example username = input("Enter...
Write below in Python Get user name from keyboard using input() function (Example username = input("Enter username:") A: What is your name? B: My name is XXXX. B: What is yours? A: My name is XXXX. A: Nice to meet you. B: Nice to meet you, too. Use print statement and use input() function
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...
Write a program compare.cpp that asks the user to input two dates (the beginning and the...
Write a program compare.cpp that asks the user to input two dates (the beginning and the end of the interval). The program should check each day in the interval and report which basin had higher elevation on that day by printing “East” or “West”, or print “Equal” if both basins are at the same level. Example: $ ./compare Enter starting date: 09/13/2018 Enter ending date: 09/17/2018 09/13/2018 West 09/14/2018 West 09/15/2018 West 09/16/2018 West 09/17/2018 West Explanation: Date East (ft)...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT