while loop in python that does not stop when condition is false

Question:

With my code (below) I want to print specific rows containing text from a data frame and ask the users to enter labels to those specific texts displayed for them. Before start annotating, I want that the users could choose the number of texts to annotate. For this purpose, I added a while loop. However, when I enter the number of texts to annotate, the loop does not stop when the condition is met. for instance, If I want to annotate say 3 texts, when I finish to annotate the 3 texts, the while loop does not stop. Can anyone help me with this? My code is below:

print( f'There are {len(d)} texts to annotate')
user=int(input('Enter number of texts you want to annotate: '))

count=0

while count < (user + 1):
  count+=1
  for i in idx:
    a=pool_df['corpus'][i]

    question=input('Enter new label(s) for this text? type Y for yes or N for no: ')
    question.lower()
    if question == 'n':
      print('see you later')
    elif question == 'y':
      print('nnEnter 1 and hit "space" if the label printed is associated with the 
        corpus, otherwise enter 0 and hit "space"')
      new_label1=float(input('x '))
      new_label2=float(input('y '))
      new_label3=float(input('z '))

obs: idx is the variable that stores the index of each row to be displayed to the users.

Asked By: ForeverLearner

||

Answers:

The while check doesn’t happen until the entire for loop completes. So you’re not counting the number of texts that you’re annotating, you’re counting the number of times you go through the entire corpus.

Instead, do the counting in the loop that goes through the corpus.

count = 0
for i in idx:
    a=pool_df['corpus'][i]

    question=input('Enter new label(s) for this text? type Y for yes or N for no: ')
    question.lower()
    if question == 'n':
      print('see you later')
    elif question == 'y':
      print('nnEnter 1 and hit "space" if the label printed is associated with the 
        corpus, otherwise enter 0 and hit "space"')
      new_label1=float(input('x '))
      new_label2=float(input('y '))
      new_label3=float(input('z '))
      count += 1
      if count >= user:
        break

Although this seems like a strange way to do it. Instead of asking for a count at the beginning, why not just ask them after each document whether there are any more documents they want to annotate?

Answered By: Barmar

thanks for the help, I am doing this way with my limited knowledge. I don’t even know how to use the def thing in python . But I wanted to use list, because it sounded convenient or so I believed. Because it needs to be like 3 different numbers and then it should in all the diagonal,rows and columns be 15. I want the user to be able to choose their number as long as they are not more than 9. (Yes I forgot to put the if statement to limit the which number it can be )

Answered By: ching_bing bong
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.