user2101517
user2101517

Reputation: 720

Multiple inputs to produce wanted output

I am trying to create a code that takes the user input, compares it to a list of tuples (shares.py) and then prints the values in a the list. for example if user input was aia, this code would return:

Please list portfolio: aia

Code  Name                   Price
AIA   Auckair                 1.50  

this works fine for one input, but what I want to do is make it work for multiple inputs. For example if user input was aia, air, amp - this input would return:

Please list portfolio: aia, air, amp

Code  Name                   Price
AIA   Auckair                 1.50
AIR   AirNZ                   5.60   
AMP   Amp                     3.22 

This is what I have so far. Any help would be appreciated!

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)))
print ('{0: <6}'.format(x[0][0]) + '{0:<20}'.format(x[0][1]) + ("{0:>8.2f}".format(x[0][2])))

shares.py contains this

EXCHANGE_DATA = [('AIA', 'Auckair', 1.5),
                 ('AIR', 'Airnz', 5.60),
                 ('AMP', 'Amp',3.22), 
                 ('ANZ', 'Anzbankgrp', 26.25),
                 ('ARG', 'Argosy', 12.22),
                 ('CEN', 'Contact', 11.22)]

Upvotes: 0

Views: 61

Answers (1)

Rishabh
Rishabh

Reputation: 765

I am assuming a to contain values in the following format 'aia air amp'

raw = a # just in case you want the original string at a later point
toDisplay = []
a = a.split() # a now looks like ['aia','air','amp']
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])))     

Essentially what I'm trying to do is

  • Split the input into a list
  • Do exactly what you were doing for a single input for each item in that list

Hope this helps!

Upvotes: 1

Related Questions