Comparing input values with csv file in Python 3.4 -


i've got simple question can't find right solution that.

i have csv file contains students' names , subjects registered: name,subject1,subject2,subject3 student1,mn1,mn2,mn3 student2,bn1,bn2,bn3 student3,mn4,mn5,mn6

student needs enter name , subject name in order check whether registered or not subject

my code:

import csv  name = input("please provide name: ") subject = input("please provide subject: ")  open('students.csv') csvfile:     reader = csv.dictreader(csvfile)     row in reader:         if (row['name'] == name , row['subject1'] == subject or  row['subject2'] == subject or row['subject3'] == subject):             print ("you registered. won't take long run vm")         else:             print ("you not registered") 

my problem gives multiple outputs me output: please provide name: student3 please provide subject: mn4 not registered not registered registered. won't take long run vm

obviously, should just: registered. won't take long run vm

could please me solve problem? thank you

note for loops in python have optional else clause execute when loop ends without break statement...

your code prints each iteration of loop. want print @ end of loop...

with open('students.csv') csvfile:     reader = csv.dictreader(csvfile)     row in reader:         if (row['name'] == name , (row['subject1'] == subject or row['subject2'] == subject or row['subject3'] == subject)):             print("you registered. won't take long run vm")             break     else:         print("you not registered") 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -