0

In python 3 I have a line asking for input that will then look in an imported dictionary and then list all their inputs that appear in the dictionary. My problem is when I run the code and put in the input it will only return the last word I input. For example the dictionary contains (AIR, AMA) and if I input (AIR, AMA) it will only return AMA. Any information to resolve this would be very helpful!

The dictionary:

EXCHANGE_DATA = [('AIA', 'Auckair', 1.50),
                 ('AIR', 'Airnz', 5.60),
                 ('AMP', 'Amp',3.22), 

The Code:

import shares
a=input("Please input")
s1 = a.replace(' ' , "")
print ('Please list portfolio: ' + a)
print (" ")
n=["Code", "Name", "Price"]
print ('{0: <6}'.format(n[0]) + '{0:<20}'.format(n[1]) + '{0:>8}'.format(n[2]))
z = shares.EXCHANGE_DATA[0:][0]
b=s1.upper()
c=b.split()
f=shares.EXCHANGE_DATA
def find(f, a):
    return [s for s in f if a.upper() in s]
x= (find(f, str(a)))
toDisplay = []
a = a.split()
for i in a:
    temp = find(f, i)
    if(temp):
        toDisplay.append(temp)
for i in toDisplay:
    print ('{0: <6}'.format(i[0][0]) + '{0:<20}'.format(i[0][1]) + ("{0:>8.2f}".format(i[0][2])))
2
  • A code sample would help, as I have no idea what you're talking about. Commented Mar 28, 2013 at 10:48
  • Updated with the code Commented Mar 28, 2013 at 10:54

1 Answer 1

1

Ok, the code seems somewhat confused. Here's a simpler version that seems to do what you want:

#!/usr/bin/env python3

EXCHANGE_DATA = [('AIA', 'Auckair', 1.50),
                 ('AIR', 'Airnz', 5.60),
                 ('AMP', 'Amp',3.22)]

user_input = input("Please Specify Shares: ")

names = set(user_input.upper().split())

print ('Listing the following shares: ' + str(names))
print (" ")

# Print header
n=["Code", "Name", "Price"]
print ('{0: <6}{1:<20}{2:>8}'.format(n[0],n[1],n[2]))

#print data
for i in [data for data in EXCHANGE_DATA if data[0] in names]:
  print ('{0: <6}{1:<20}{2:>8}'.format(i[0],i[1],i[2]))

And here's an example of use:

➤ python3 program.py 
Please Specify Shares: air amp
Listing the following shares: {'AMP', 'AIR'}

Code  Name                   Price
AIR   Airnz                    5.6
AMP   Amp                     3.22

The code sample you provided actually does what was expected, if you gave it space separated quote names.

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for you help! this certainly makes it clearer, is it at all possible for it to work in the same way if the input contained commas? (for example air, amp, aia) thanks again
you can do that by using the re module to split the input. for example, replace names = set(user_input.upper().split()) by names = set(re.split("[\t., ]*", user_input)) have a look at the documentation for the re module to learn more.
Also note that the solution above is computationally expensive (goes through the list of every share price) and there might be better ways of doing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.