checking if input1 and input2 is in list of strings

Question:

I am trying to print names that equal to inputs

for example :

if input1 = 'A' and input2 = 'G' 
    print("Arsalan Ghasemi")

so my code works but for some names it’s not working

if input = ‘S’ and second input = ‘S’ again
it will print 3 names that has ‘S’ in it even they are lowercases

here my code

names = ['Arsalan Ghasemi', 'Ali Bahonar', 'Negin Soleimani', 'Farzaneh Talebi', 'Sina Ghahremani',
         'Saman Sorayaie', 'Abtin Tavanmand', 'Masoud Jahani', 'Roya Pendar', 'Zeynab Arabi',
         'Amirhossein Tajbakhsh', 'Aria Irani']


def names_with_input(input1, input2):
    for i in range(len(names)):
        if input1.upper() in names[i] and input2.upper() in names[i]: 
            print(names[i])

first = input('Enter first letter: ')
last = input('Enter last letter: ')

names_with_input(first, last)

I thought it’s only check upper cases but seems it’s not
how I can fix this when inputs are ‘S’ and ‘S’, it should only give me ‘Saman Sorayaie’

Asked By: Ali Ebadi

||

Answers:

You may need to check the first character of each string separated by the space between name and surname:

names = ['Arsalan Ghasemi', 'Ali Bahonar', 'Negin Soleimani', 'Farzaneh Talebi', 'Sina Ghahremani',
         'Saman Sorayaie', 'Abtin Tavanmand', 'Masoud Jahani', 'Roya Pendar', 'Zeynab Arabi',
         'Amirhossein Tajbakhsh', 'Aria Irani']


def names_with_input(input1, input2):
    for i in range(len(names)):
        s = names[i].split(" ")
        if input1.upper() == s[0][0] and input2.upper() == s[1][0]: 
            print(names[i])

first = input('Enter first letter: ')
last = input('Enter last letter: ')

names_with_input(first, last)

Output:

Enter first letter: A
Enter last letter: T
Abtin Tavanmand
Amirhossein Tajbakhsh
Answered By: Cardstdani

Check the initials only instead of checking if the initial occurs inside the whole name. Split the first, last using str.split().

names = ['Arsalan Ghasemi', 'Ali Bahonar', 'Negin Soleimani', 'Farzaneh Talebi', 'Sina Ghahremani',
         'Saman Sorayaie', 'Abtin Tavanmand', 'Masoud Jahani', 'Roya Pendar', 'Zeynab Arabi',
         'Amirhossein Tajbakhsh', 'Aria Irani']


def names_with_input(input1, input2):
    for n in names:
        n1, n2 = n.split()
        if input1.upper() == n1[0] and input2.upper() == n2[0]: 
            print(n)
            break

first = input('Enter first letter: ')
last = input('Enter last letter: ')

names_with_input(first, last)
Answered By: Britec
names = ['Arsalan Ghasemi', 'Ali Bahonar', 'Negin Soleimani', 'Farzaneh Talebi', 'Sina Ghahremani',
         'Saman Sorayaie', 'Abtin Tavanmand', 'Masoud Jahani', 'Roya Pendar', 'Zeynab Arabi',
         'Amirhossein Tajbakhsh', 'Aria Irani']


def names_with_input(input1, input2):
    for name in names:
        first_name, last_name = name.split()
        if input1.upper() == first_name[0] and input2.upper() == last_name[0] : 
            print(name)
names_with_input('s','s')

hope this will help for you

Answered By: iamniki
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.