[Python] My code is not printing the correct values

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have a code that runs through data.csv file, and it prints out values. But the values do not fit the test cases.

    import csv
    filePath="data.csv"
    with open(filePath) as csvfile:
    reader=csv.DictReader(csvfile)

    print("========================================================")
    print("Display item stock level above certain amount")
    print("========================================================")
    amount = input("Enter amount : ")
    print("========================================================")

    for row in reader:
    if (row['STOCK LEVEL'] >= amount):
    print("PRODUCT ID : {0}\nNAME : {1}\nLEVEL : {2}".format(row['PRODUCT ID'],row['NAME'],row['STOCK LEVEL']))
    print()


    My codes asks an int input from the user, and would read through the file and print the details of the item, eg Product ID, name, stock level.

    but when i enter 8, it should print the details for stock level above 8 and 12, but it only prints the details for 8. When i ask for 12, it would print the details for every item, (5, 7, 8, 12), which is wrong.

    Continue reading...

Compartilhe esta Página