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'
CodePudding user response:
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
CodePudding user response:
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)
CodePudding user response:
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() in first_name[0] and input2.upper() in last_name[0] :
print(name)
names_with_input('s','s')
hope this will help for you