-1

I've been trying to code a game using python where I need the player's answer to be accepted as the same as the correct answer however regardless of whether or not the answer is correct it still proceeds as if the answer is incorrect

This is the code:

def EasySongs1(easy):
 f=open('easy.txt')
 lines=f.readlines()
 songline=random.randrange(1,30)
 answereasy=(lines[songline])
 f=open('easyq.txt')
 lines=f.readlines()
 questioneasy=(lines[songline])
 print(questioneasy)
 life1=str(input("Guess the song "))
 if life1 is answereasy:
   return("good job")
 if life1 is not answereasy:
   life2=str(input("2 lives left,Guess again "))
   if life2 is not answereasy:
     life3 = str(input("last chance, guess again "))
     if life3 is not answereasy:
       sys.exit("you messed up")

I've already tried both is and == Anyone got any idea on how to fix this?

  • 1
    Use `!=` instead of `is not`. – Thierry Lathuille Oct 20 '21 at 18:37
  • `is` or `is not` is not the same thing as checking equality (via `==` or `!=`). `is` is checking if two objects have the same internal ID. You could also be converting the strings to uppercase or lowercase to make sure that casing isn't a factor. If they still aren't considered equal (I can't test your code since it's not a [mre]), then I suggest printing out the contents and length of the strings you're checking, to make sure they're actually the same and don't contain anything like extra whitespace or whatever. – Random Davis Oct 20 '21 at 18:38
  • == and != were the first things i tried and they didn't work so i changed it to is and is not – Rocketboosters Oct 20 '21 at 19:10

1 Answers1

0

Use == instead of is and != instead of is not. is is generally used when None is used.