-1

I am writing a program which predicts stock prices and I need to search a dictionary that I have created filled with names of companies and the ticker names of the companies which is what I need to return to use Quandl to retrieve the stock prices.

Here is how I created the dictionary:

cnames= pd.read_csv('secwiki_tickers.csv')
cnamesDict= pd.Series(cnames.Ticker.values, index=cnames.Name).to_dict()#Fills a dictionary with the 
csv file keys are company names values are ticker names

here is how I'm searching the dictionary and getting TypeError: argument of type 'float' is not iterable error:

user_cname = input("Which company would you like to predict stock prices for?\n")
def searchForName(dictToSearch, lookup):
  for k,v in dictToSearch.items():
    if user_cname in k:    // here is where the error flags
      return v 





print(searchForName(cnamesDict, user_cname)) 

Any help is appreciated. CSV file linkLINK

burnie5749
  • 21
  • 3
  • `k` is a float - you cant iterate `float` - thats your EXACT error message. use `print(k)` to verify your key is a float. Dont do `if user_cname in k:` - it might be `if user_cname == k:` if you name users by float. – Patrick Artner Apr 26 '20 at 11:52
  • Yeah, because you are looking for user_cname in k, while k is not iterable. What is your key basically? – Ayush Pallav Apr 26 '20 at 11:52
  • It helps us to get a [mre] so we can create the error ourself. Hardcode some data as dict and [edit] your post. – Patrick Artner Apr 26 '20 at 11:53
  • which is why i am confused because the key is a string?? ill put a link to the csv file now – burnie5749 Apr 26 '20 at 11:55
  • also when I do ```print(k)``` it prints all of the keys(which are strings) – burnie5749 Apr 26 '20 at 14:30

1 Answers1

-1

You should directly lookup in the dictionary for the company name using the .get method, that is

def searchForName(dictToSearch):
   return dictToSearch.get(user_cname, 'not found')