简体   繁体   中英

Why does the loop not get executed even though the conditions are satisfied?

I am working on a program that updates/changes the element in a CSV folder. It first reads the CSV file list by list and checks if the serial number matches the one given by the user. If it does then it asks the user for the next element to change.

But in my program, the first IF loop is not running even though the conditions are satisfied. The serial number I tried entering was present in the list but it still wouldn't execute. Please look into this problem.

Here is my code.

def update():
    f=open("daily.csv",'r')
    r=csv.reader(f)
    sno=int(input("WHICH RECORD DO YOU WANT TO UPDATE? (ENTER THE SERIAL NO.)"))
    next(r)
    for i in r:
        if i[0]==sno:
            ch=int(input("1.CHANGE CATEGORY (PRESS 1)2.CHANGE AMOUNT (PRESS 2)3.CHANGE DATE (PRESS 3)"))
            if ch==1:
                nc=input("PLEASE ENTER NEW CATEGORY")
                i[1]=nc
                print(i)
            elif ch==2:
                na=int(input("PLEASE ENTER THE NEW AMOUNT"))
                i[2]=na
                print(i)
            elif ch==3:
                nd=int(input("PLEASE ENTER THE NEW DATE"))
                i[3]=nd
                print(i)
        else:
            print("INVALID SERIAL NO")
    f.close()

I believe the problem is that the values from the file are being read as string. Try converting the s[0] to int first then compare. Also, if you are using an IDE like pycharm, then use the inbuilt debugger to see the values and why they fail. The problem may also be with the index of 's' that you are trying to access. If you don't have a debugger then try printing the value and the type of the values as that might help in the debugging process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM