I have two variables that I know are equal but my if statement does not recognise this?












3















This is my code:



bookings = ['blue,red', 'green,orange', 'yellow, purple']
number = 0
b = 0
c = 1

file_test = open('test_1.txt' , 'wt')

results_song =

for item in bookings:
words = bookings[number].split(',')
results_song.append(words[0])
results_song.append(words[1])

number = number + 1

results_song_str = 'n'.join(results_song)
print(results_song_str)

file_test.write(results_song_str)

file_test.close()

file_test = open('test_1.txt' , 'r')

line = file_test.readlines()


for item in bookings:

line_1 = line[b]
line_2 = line[c]
answer = input('If first word is then what is the second word')

if answer == line_2:
print('correct')
else:
print('wrong')

b = b + 2
c = c + 2


However the code will not recognise that answer is equal to line_2. I cannot figure out why this is happening. I have checked that c is the correct number and that line_2 is the same as answer. But I did notice that when I ran the code while printing answer and line_2 that this would return:



red

red


but I never put a new line feature in here.



Any help would be much appreciated as I need to use this code for a school assignment.










share|improve this question




















  • 2





    before your if statement, print(repr(answer), repr(line_2)), I suspect there is some whitespace not taken into account

    – juanpa.arrivillaga
    Nov 14 '18 at 18:22






  • 1





    [Minor Issue] In your first for loop, you're not using item, it becomes redundant. Use it instead of bookings[number].

    – TrebuchetMS
    Nov 14 '18 at 18:23


















3















This is my code:



bookings = ['blue,red', 'green,orange', 'yellow, purple']
number = 0
b = 0
c = 1

file_test = open('test_1.txt' , 'wt')

results_song =

for item in bookings:
words = bookings[number].split(',')
results_song.append(words[0])
results_song.append(words[1])

number = number + 1

results_song_str = 'n'.join(results_song)
print(results_song_str)

file_test.write(results_song_str)

file_test.close()

file_test = open('test_1.txt' , 'r')

line = file_test.readlines()


for item in bookings:

line_1 = line[b]
line_2 = line[c]
answer = input('If first word is then what is the second word')

if answer == line_2:
print('correct')
else:
print('wrong')

b = b + 2
c = c + 2


However the code will not recognise that answer is equal to line_2. I cannot figure out why this is happening. I have checked that c is the correct number and that line_2 is the same as answer. But I did notice that when I ran the code while printing answer and line_2 that this would return:



red

red


but I never put a new line feature in here.



Any help would be much appreciated as I need to use this code for a school assignment.










share|improve this question




















  • 2





    before your if statement, print(repr(answer), repr(line_2)), I suspect there is some whitespace not taken into account

    – juanpa.arrivillaga
    Nov 14 '18 at 18:22






  • 1





    [Minor Issue] In your first for loop, you're not using item, it becomes redundant. Use it instead of bookings[number].

    – TrebuchetMS
    Nov 14 '18 at 18:23
















3












3








3


1






This is my code:



bookings = ['blue,red', 'green,orange', 'yellow, purple']
number = 0
b = 0
c = 1

file_test = open('test_1.txt' , 'wt')

results_song =

for item in bookings:
words = bookings[number].split(',')
results_song.append(words[0])
results_song.append(words[1])

number = number + 1

results_song_str = 'n'.join(results_song)
print(results_song_str)

file_test.write(results_song_str)

file_test.close()

file_test = open('test_1.txt' , 'r')

line = file_test.readlines()


for item in bookings:

line_1 = line[b]
line_2 = line[c]
answer = input('If first word is then what is the second word')

if answer == line_2:
print('correct')
else:
print('wrong')

b = b + 2
c = c + 2


However the code will not recognise that answer is equal to line_2. I cannot figure out why this is happening. I have checked that c is the correct number and that line_2 is the same as answer. But I did notice that when I ran the code while printing answer and line_2 that this would return:



red

red


but I never put a new line feature in here.



Any help would be much appreciated as I need to use this code for a school assignment.










share|improve this question
















This is my code:



bookings = ['blue,red', 'green,orange', 'yellow, purple']
number = 0
b = 0
c = 1

file_test = open('test_1.txt' , 'wt')

results_song =

for item in bookings:
words = bookings[number].split(',')
results_song.append(words[0])
results_song.append(words[1])

number = number + 1

results_song_str = 'n'.join(results_song)
print(results_song_str)

file_test.write(results_song_str)

file_test.close()

file_test = open('test_1.txt' , 'r')

line = file_test.readlines()


for item in bookings:

line_1 = line[b]
line_2 = line[c]
answer = input('If first word is then what is the second word')

if answer == line_2:
print('correct')
else:
print('wrong')

b = b + 2
c = c + 2


However the code will not recognise that answer is equal to line_2. I cannot figure out why this is happening. I have checked that c is the correct number and that line_2 is the same as answer. But I did notice that when I ran the code while printing answer and line_2 that this would return:



red

red


but I never put a new line feature in here.



Any help would be much appreciated as I need to use this code for a school assignment.







python python-3.x if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 20:40







Alex Ganvir

















asked Nov 14 '18 at 18:16









Alex GanvirAlex Ganvir

234




234








  • 2





    before your if statement, print(repr(answer), repr(line_2)), I suspect there is some whitespace not taken into account

    – juanpa.arrivillaga
    Nov 14 '18 at 18:22






  • 1





    [Minor Issue] In your first for loop, you're not using item, it becomes redundant. Use it instead of bookings[number].

    – TrebuchetMS
    Nov 14 '18 at 18:23
















  • 2





    before your if statement, print(repr(answer), repr(line_2)), I suspect there is some whitespace not taken into account

    – juanpa.arrivillaga
    Nov 14 '18 at 18:22






  • 1





    [Minor Issue] In your first for loop, you're not using item, it becomes redundant. Use it instead of bookings[number].

    – TrebuchetMS
    Nov 14 '18 at 18:23










2




2





before your if statement, print(repr(answer), repr(line_2)), I suspect there is some whitespace not taken into account

– juanpa.arrivillaga
Nov 14 '18 at 18:22





before your if statement, print(repr(answer), repr(line_2)), I suspect there is some whitespace not taken into account

– juanpa.arrivillaga
Nov 14 '18 at 18:22




1




1





[Minor Issue] In your first for loop, you're not using item, it becomes redundant. Use it instead of bookings[number].

– TrebuchetMS
Nov 14 '18 at 18:23







[Minor Issue] In your first for loop, you're not using item, it becomes redundant. Use it instead of bookings[number].

– TrebuchetMS
Nov 14 '18 at 18:23














1 Answer
1






active

oldest

votes


















1














Debugging by printing



# ...

for item in bookings:
line_1 = line[b]
line_2 = line[c]

print("Your Answer:", repr(answer))
print("Actual Answer:", repr(line_2))

# ...


gives



Your Answer: 'red'
Actual Answer: 'redn'


Aha! A sneaky newline character! Seems like when the program was reading text from the file and splitting the lines, it saved the newline character for you. How sweetly annoying. : |



To remove it, you can use the str.replace() method



# ...

for _ in range(len(bookings)): # I took the freedom to modify the loop conditions

line_1 = line[b].replace('n','')
line_2 = line[c].replace('n','')

# ...


or change the way lines are read from the file, manually splitting the lines using the str.split() method



# ...

with open('test_1.txt' , 'r') as file_test:
line = file_test.read().split('n')

for _ in range(len(bookings)):
line_1 = line[b]
line_2 = line[c]

# ...




Credit goes to @juanpa.arrivillaga for suggesting the use of repr() to check values.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306474%2fi-have-two-variables-that-i-know-are-equal-but-my-if-statement-does-not-recognis%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Debugging by printing



    # ...

    for item in bookings:
    line_1 = line[b]
    line_2 = line[c]

    print("Your Answer:", repr(answer))
    print("Actual Answer:", repr(line_2))

    # ...


    gives



    Your Answer: 'red'
    Actual Answer: 'redn'


    Aha! A sneaky newline character! Seems like when the program was reading text from the file and splitting the lines, it saved the newline character for you. How sweetly annoying. : |



    To remove it, you can use the str.replace() method



    # ...

    for _ in range(len(bookings)): # I took the freedom to modify the loop conditions

    line_1 = line[b].replace('n','')
    line_2 = line[c].replace('n','')

    # ...


    or change the way lines are read from the file, manually splitting the lines using the str.split() method



    # ...

    with open('test_1.txt' , 'r') as file_test:
    line = file_test.read().split('n')

    for _ in range(len(bookings)):
    line_1 = line[b]
    line_2 = line[c]

    # ...




    Credit goes to @juanpa.arrivillaga for suggesting the use of repr() to check values.






    share|improve this answer






























      1














      Debugging by printing



      # ...

      for item in bookings:
      line_1 = line[b]
      line_2 = line[c]

      print("Your Answer:", repr(answer))
      print("Actual Answer:", repr(line_2))

      # ...


      gives



      Your Answer: 'red'
      Actual Answer: 'redn'


      Aha! A sneaky newline character! Seems like when the program was reading text from the file and splitting the lines, it saved the newline character for you. How sweetly annoying. : |



      To remove it, you can use the str.replace() method



      # ...

      for _ in range(len(bookings)): # I took the freedom to modify the loop conditions

      line_1 = line[b].replace('n','')
      line_2 = line[c].replace('n','')

      # ...


      or change the way lines are read from the file, manually splitting the lines using the str.split() method



      # ...

      with open('test_1.txt' , 'r') as file_test:
      line = file_test.read().split('n')

      for _ in range(len(bookings)):
      line_1 = line[b]
      line_2 = line[c]

      # ...




      Credit goes to @juanpa.arrivillaga for suggesting the use of repr() to check values.






      share|improve this answer




























        1












        1








        1







        Debugging by printing



        # ...

        for item in bookings:
        line_1 = line[b]
        line_2 = line[c]

        print("Your Answer:", repr(answer))
        print("Actual Answer:", repr(line_2))

        # ...


        gives



        Your Answer: 'red'
        Actual Answer: 'redn'


        Aha! A sneaky newline character! Seems like when the program was reading text from the file and splitting the lines, it saved the newline character for you. How sweetly annoying. : |



        To remove it, you can use the str.replace() method



        # ...

        for _ in range(len(bookings)): # I took the freedom to modify the loop conditions

        line_1 = line[b].replace('n','')
        line_2 = line[c].replace('n','')

        # ...


        or change the way lines are read from the file, manually splitting the lines using the str.split() method



        # ...

        with open('test_1.txt' , 'r') as file_test:
        line = file_test.read().split('n')

        for _ in range(len(bookings)):
        line_1 = line[b]
        line_2 = line[c]

        # ...




        Credit goes to @juanpa.arrivillaga for suggesting the use of repr() to check values.






        share|improve this answer















        Debugging by printing



        # ...

        for item in bookings:
        line_1 = line[b]
        line_2 = line[c]

        print("Your Answer:", repr(answer))
        print("Actual Answer:", repr(line_2))

        # ...


        gives



        Your Answer: 'red'
        Actual Answer: 'redn'


        Aha! A sneaky newline character! Seems like when the program was reading text from the file and splitting the lines, it saved the newline character for you. How sweetly annoying. : |



        To remove it, you can use the str.replace() method



        # ...

        for _ in range(len(bookings)): # I took the freedom to modify the loop conditions

        line_1 = line[b].replace('n','')
        line_2 = line[c].replace('n','')

        # ...


        or change the way lines are read from the file, manually splitting the lines using the str.split() method



        # ...

        with open('test_1.txt' , 'r') as file_test:
        line = file_test.read().split('n')

        for _ in range(len(bookings)):
        line_1 = line[b]
        line_2 = line[c]

        # ...




        Credit goes to @juanpa.arrivillaga for suggesting the use of repr() to check values.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 19:28

























        answered Nov 14 '18 at 19:21









        TrebuchetMSTrebuchetMS

        2,3921622




        2,3921622






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306474%2fi-have-two-variables-that-i-know-are-equal-but-my-if-statement-does-not-recognis%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()