-2

I have the following dataset and I want to manually check with value in the column Name is the same of the value in the column Factory for the same rows.
I developed the following code reading the documentation around, but does not fully work. I want to make sure that there are the following characteristics:

  1. If I enter 1 it means that is all good, the name match and I can proceed to the next row
  2. If I enter 0 it means that the names do not match but I can anyway proceed to the next row
  3. If I enter 9 I am tired of checking and I want the whole loop to end.
  4. If I enter something that is not 1, 0 or 9, the loop asks me to re-enter the value
data = {'Name':  ['Facotry One', 'Second value', 'Match'],
        'Factory': ['Footwear Limited', 'Footweat not limited', 'Match']}
    
df_test = pd.DataFrame (data, columns = ['Name','Factory'])

df_test
english_matches = []

for index, row in df_test.iterrows():
    print('Is ',  row['Name'], ' the same as ', row['Factory' ])
    while True:
        try:
            match = input("Enter 1 for match, 0 for mistmatch, 9 to exit the loop: ")
        
        if match not in (1, 0, 9):
            except ValueError:
            print("Sorry, I didn't understand that.")
            continue
                
        else:
            english_matches.append(match)
            break
        
    if match == 9:
            Break
        
    else:
            print("You are still here")

Error


  File "<ipython-input-16-81bdfa6d212f>", line 15
    if match not in (1, 0, 9):
    ^
SyntaxError: invalid syntax
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Filippo Sebastio
  • 1,112
  • 1
  • 12
  • 23

1 Answers1

0

Your code needs to be rearranged.

You should start by importing pandas:

import pandas as pd

Then you set your variables:

data = {'Name': ['Factry One', 'Second value', 'Match'],
        'Factory': ['Footwear Limited', 'Footweat not limited', 'Match']}

df_test = pd.DataFrame(data, columns=['Name', 'Factory'])

english_matches = []

You start your loop:

for index, row in df_test.iterrows():

    print('Is ', row['Name'], ' the same as ', row['Factory'])

    while True:

You need to cast the match variable as an int otherwise it will not match later in the if condition:

        match = int(input("Enter 1 for match, 0 for mismatch, 9 to exit the loop: "))

As for the checking the answer is within expectation you should choose between checking with a condition or with the try-except. I kept it with a condition:

        if match not in (1, 0, 9):
            print("Sorry, I didn't understand that.")

        elif match == 9:
            break

        else:
            english_matches.append(match)
            break

I didn't understand when you wanted to print:

print("You are still here")
Peter Ark
  • 244
  • 1
  • 6