Joseph Senjel
Joseph Senjel

Reputation: 13

Conditional statement not working to show if two strings are equal. (basic python)

I am just starting out with python, this is part of my first program. The if statement at the end always prints 'Please try again' even if the strings are identical and should output 'Well done'.

I have added print(usranswer) print(correctans) to make sure both strings are equal, which they are, i have also added usranswer.strip() correctans.strip() to remove any non existent white spaces, and the code is still not outputting the right result.

Any other suggestions would be helpful, Thanks.

fail = 0
Qnumber = 1

while fail != 2:

    import random

    q = random.randint(1,5)

    with open("answers.txt", "r") as answers:
        for _ in range(q):
            answer = answers.readline()

    with open("questions.txt", "r") as initials:
        for _ in range(q):
            question = initials.readline()

    print("question number")
    print(str(Qnumber))

    print('Please guess the name of this song, the name of the artist and the first letter in each word of the song title are below')

    print(question)

    usranswer = str(input())
    correctans = str(answer)

    usranswer.strip()
    correctans.strip()

    print(usranswer)
    print(correctans)

    if correctans == usranswer:
        print('Well done')
        score = score + 3
    else:
        fail = fail + 1
        print('Please try again')

Even when both correctans and userans are exactly equal it still prints Please try again when it should print Well Done

Upvotes: 0

Views: 72

Answers (1)

user11283167
user11283167

Reputation:

Your .strip() calls return the stripped version of the string but you don't assign them to anything

You could try this

usranswer = input().strip()
correctans = answer.strip()

Upvotes: 1

Related Questions