0

I've been facing a problem lately with my assignment. I was working on a part that would take input from a user and according to that input, it would print a certain thing. This should be an ongoing loop where the system keeps asking for input until the user would eventually type 99 which will end the loop. Although it works for the 1st time, when I re-input a value the system would no longer print what it is supposed to print corresponding to the value of the input, and for the value of input equal to 2, it wouldn't print anything at all.

import csv                       #importing the module
csv_file=open('ssc2.csv','r')
csv_reader=csv.reader(csv_file)
while True:
    User_option=int(input('Your option is:'))
    if User_option== 99:
        break
    elif User_option== 1:
        Sum_books=0
        for i in csv_reader:
            Sum_books=Sum_books+1
        print('reading data was successful')
        print('current number of books in stock:',Sum_books-1)
   elif User_option == 2:
        for i in csv_reader:
            print(i)

Output

Only works for the first time I Input

Where for the input value equal to 2, it should print a couple of lists which actually works the first time but then when the system re-asks for input and I re-input it doesn't work. And for an input value of 1, it should print a message and then a calculation of the no. of books in stock and again it works perfectly the first time but the second time it still prints the message but for the calculation its wrong.

I would be grateful if anyone would help me with this Thank you!

  • 1
    A file and a csv reader are one-pass iterators. Once you have read though them (in the first `while` iteration) you cannot read through them again. – MisterMiyagi Dec 18 '22 at 05:26
  • BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Dec 18 '22 at 05:31

0 Answers0