2021年3月27日星期六

Unwanted run through else/if not loop

This is a small but crucial error because it affects the code where you have to give a specific 'not found message'....but the problem might also lie with the fact that i'm using csv files. In this code I specified that 'not found' must be printed only if the record is not found, but it keeps printing anyways.

CODE

import csv  with open('data.csv','w') as f:      ans='y'      while ans=='y':          s=csv.writer(f)          g=int(input("Enter GR number:"))          n=input("Enter name:")          m=float(input("Enter total marks:"))          s.writerow([g,n,m])          print()          ans=input('Enter more?')            with open('data.csv','r') as f:      print("SEARCHING FOR GR NUMBER")      print()      gr=int(input('Enter GR number to search:'))      s=list(csv.reader(f))      for i in s:          if str(gr) in i:              print(i)      if str(gr) not in i:          print('Record not found')  

OUTPUT

Enter GR number:234  Enter name:Mia  Enter total marks:80    Enter more?y  Enter GR number:456  Enter name:Mark  Enter total marks:80    Enter more?n  SEARCHING FOR GR NUMBER    Enter GR number to search:234  ['234', 'Mia', '80.0']  Record not found  

I have even tried other methods to print not found...but the same error occurs anyways lol. Any help appreciated. Thanks!

https://stackoverflow.com/questions/66837973/unwanted-run-through-else-if-not-loop March 28, 2021 at 10:50AM

没有评论:

发表评论