jeudi 5 décembre 2019

How to repeat the task by using while loop (search the information from a text file)?

I wrote a program to search the text file, it seems fine when I search another item with its record number in increasing order, but when I search in decreasing order, it doesn't run fine, what's the problem?

The code is:

infile=open("inventory.txt","r") 

while(True):
    searching=str(input("Please enter Record number:")) 

    lineCount=0 #counting which line the program going to start display
    for line in infile:
        lineCount+=1
        if searching==line.rstrip():
            break

    itemname=infile.readline() 
    print("Item name: ",itemname,end="") 

    while searching!="": 

        itemnum=infile.readline() 
        category=infile.readline() 
        quantity=infile.readline() 
        weight=infile.readline() 
        recipient=infile.readline() 
        finaldestination=infile.readline() 
        deliverystatus=infile.readline() 

        print("Item number: ",itemnum,end="") 
        print("Category: ",category,end="")
        print("Quantity: ",quantity,end="") 
        print("Weight: ",weight,end="") 
        print("Recipient: ",recipient,end="") 
        print("Final destination: ",finaldestination,end="") 
        print("Delivery status: ",deliverystatus) 
        break 

    cont=str(input("Do you want to search another item? (y/n)"))

    if cont.lower()=="y": 
        continue
    elif cont.lower()=="n":
        break
    else:
        print("Please make sure input is vaild.") 

    break 

infile.close()

The result is:

Please enter Record number:1001
Item name: Orange
Item number: 235524
Category: Fruit
Quantity: 1
Weight: 1.8 kg
Recipient: Eason
Final destination: HK
Delivery status: Delivery

Do you want to search another item? (y/n)y
Please enter Record number:1003
Item name: Banana
Item number: 75524
Category: Fashion
Quantity: 3
Weight: 0.6 kg
Recipient: Chris
Final destination: Fortress hill
Delivery status: Warehouse
Do you want to search another item? (y/n)y
Please enter Record number:1002
Item name: Item number: Category: Quantity: Weight: Recipient: Final destination: Delivery status:
Do you want to search another item? (y/n)

Here's the example of the data file:

1001
Orange
235524
Fruit
1
1.8 kg
Eason
HK
Delivery

1002
Apple
305522
Electronic
2
4.2 kg
Mary
NY
Arrival

1003
Banana
75524
Fashion
3
0.6 kg
Chris
Fortress hill
Warehouse

Aucun commentaire:

Enregistrer un commentaire