-4

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'

Ali Ebadi
  • 11
  • 4
  • In your own words, where the code says `if input1.upper() in names[i] and input2.upper() in names[i] == names[i]:`, what is the intended purpose of the `== names[i]` part? How do you intend for the logic in this line to work, step by step? Please read [ask] and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/, and try to think carefully about the **exact** steps the code needs to take, step by step, in order to solve the problem. – Karl Knechtel Jul 01 '22 at 07:22
  • i=0 and name[0] --> 'Arsalan Ghasemi' || is 'A' in 'Arsalan Ghasemi' ? yes okay next input Is 'G' in 'Arsalan Ghasemi' ? yes okay now print 'Arsalan Ghasemi', i am thinking wrong? – Ali Ebadi Jul 01 '22 at 07:25
  • Okay, so the first part of that is covered by `if input1.upper() in names[i]`, right? And the second part is covered by `input2.upper() in names[i]`, right? And then what is this leftover `== names[i]` part for? – Karl Knechtel Jul 01 '22 at 07:27
  • Oh, It was mistake when I was copy pasting it here sorry D: – Ali Ebadi Jul 01 '22 at 07:29
  • Now, as far as your question goes: Is 'S' in 'Negin Soleimani'? Is 'S' in 'Negin Soleimani'? (I did not stutter; if you give two 'S' inputs, then it will try the same test twice, right?) I think it is: I think 'Soleimani' starts with an S, so I expect that 'S' will be found in 'Negin Soleimani'. So, the result is not confusing, is it? In that case - maybe this is not actually what you want to test. I think you want to test: "Is 'S' in 'Negin'? Is 'S' in 'Soleimani'?" In fact, maybe it is even more specific than that. Please try to think about it carefully. – Karl Knechtel Jul 01 '22 at 07:29
  • okay i get it now thanks man – Ali Ebadi Jul 01 '22 at 07:34

3 Answers3

3

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)
Britec
  • 31
  • 2
2
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

iamniki
  • 553
  • 3
  • 11
1

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
Cardstdani
  • 4,999
  • 3
  • 12
  • 31