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

Time:07-08

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('\n\nEnter 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.

CodePudding user response:

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('\n\nEnter 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?

  • Related