0
a=  ['Emre', 'Hacettepe', 'University', 'Computer', 'Engineering']
b=  ['Kerem', 'METU', 'Architecture']
c=  ['Leyla', 'Ankara', 'University', 'Physics']
d=  ['Sami', 'Bilkent', 'University', 'Civil', 'Engineering'] 

#S=input list that i wrote as argument but i didn't add here all the codes because it is too long.Etc input: Emre,Ahmet

x=len(S)
for i in range(0,x):
    list=[]
    if a[0]== S[i] :
        print('Name: '+ (a[0])+', University: '+a[1]+' '+a[2]+','+a[3]+' '+a[4],end=' ')
        list.append(S[i])
    if h[0]== S[i]:
        print('Name: '+ (h[0])+', University: '+h[1]+' '+h[2]+','+h[3]+' '+h[4],end=' ')
        list.append(S[i])
    if e[0]==S[0]:
        print('Name: '+e[0]+', University: '+e[1]+','+e[2],end=' ')
        list.append(S[i])
    if f[0]==S[i]:
        print('Name: '+f[0]+', University: '+f[1]+' '+f[2]+','+f[3],end=' ')
        list.append(S[i])
    if a[0] or h[0] or e[0] or f[0]!= S[i] and S[i] not in list:
        print('No record of '+"'"+S[i]+"'"+' was found!',end=' ')

In last 'if' statement I just only want to write the nonexisting ones but i am receiving an wrong output.

Name: Emre, University: Hacettepe University,Computer Engineering No record of 'Emre' was found! No record of 'Ahmet' was found!

No record of 'Emre' shouldn't be in the output. Where is my mistake?

Alexandra
  • 27
  • 5

1 Answers1

1

You have the logic wrong.

if a[0] or h[0] or e[0] or f[0]!= S[i] and S[i] not in list:
        print('No record of '+"'"+S[i]+"'"+' was found!',end=' ')

should be

if a[0] != S[i] and h[0] != S[i] and e[0] != S[i] and f[0] != S[i] and S[i] not in list:
        print('No record of '+"'"+S[i]+"'"+' was found!',end=' ')

A clearer approach would be

if S[i] not in [a[0],b[0],c[0],d[0]]:
        print('No record of '+"'"+S[i]+"'"+' was found!',end=' ')

(note S[i] will never be in list if it's not in a-d)

an even better approach would be

students = {}
students['Emre'] = {'University':'Hacettepe', 'Course': 'Computer Engineering'}
students['Kerem'] =  {'University':'METU', 'Course': 'Architecture'}

for name in students:
    print(name,students[name]['University'],", University, ",students[name]['Course'])
JeffUK
  • 4,107
  • 2
  • 20
  • 34
  • 1
    an even even better approach would be a 'Student' Class with attributes of 'university' and 'course' – JeffUK Dec 11 '20 at 19:35