Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a program function() that takes a string which represents the name of a f

ID: 3644247 • Letter: I

Question

I need a program function() that takes a string which represents the name of a file as a parameter. In this file, it contains company names and stock symbols. Also, the company name appears on one line and the stock symbol for that company is on the next line. They appear in all caps. The function() program reads the file and then it will store the name of the company and the stock symbols in a DICTIONARY. Then it gives an interface to the user so that the user can get the stock symbol for the given company. It should be ok in lowercase, uppercase, mixed and still be able to get the symbol and when a blank company name is entered, the program should end.

- This is how it looks in the word file.

ACTIVISION INC

ATVI

ADOBE SYS INC

ADBE

ALTERA CORP

ALTR

AMAZON

AMZN

With many more also.

Example of how it runs


>>> function('nasdaq.txt') Enter a company name: Yahoo Ticker symbol: YHOO Enter a company name: Google INC Ticker symbol: GOOG Enter a company name: Thank you for using the ticker lookup system!

Explanation / Answer

def function(filename):
    f=open(filename)
    names=[]
    codes=[]
    linenumber=0
    #First read in the file
    for line in f:
        linenumber=linenumber+1
        if linenumber%2:
            names.append(line.lower()) #lower the case so case doesn't matter
        else:
            codes.append(line)
    f.close()
    # Now make the dictionary

    dic={}
    for i in range(len(names)):
        dic[names[i]]=codes[i]

    done = False
    while not done:
        cname=raw_input("Enter a company name:")

        if cname="":
            print "Thank you for using the ticker lookup system"
            done = True
            continue
        cname = cname.lower() # So case doesn't matter
        if cname in names:
            print "Ticker symbol:", dic[cname]
        else:
            print "No such company."

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote