0

Both of strings from userInput and fcon matches, it is still unable to print the "matched" message.

I have tried comparing two strings from input() it work.

userInput = input("First String")

with open("file1", mode ="r") as nameDb:
    fcon = nameDb.readline()

print(type(fcon),fcon)
print(type(userInput), userInput)


if userInput is fcon:
    print("Matched")
else:
    print("No match")


Makyen
  • 31,849
  • 12
  • 86
  • 121
  • 1
    `is` check is a bad idea. If you are to compare string equality, use `==`. – Austin Mar 22 '20 at 06:19
  • 1
    Use `==`. But also be careful about the possible new line character coming from your `readline()` call. You might call a `strip()` after that. – Seleme Mar 22 '20 at 06:22
  • Does this answer your question? [Is there a difference between "==" and "is"?](https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is) – FObersteiner Mar 22 '20 at 08:56
  • You should ask another question instead of editing an exist one if you have anything new to ask. The edition currently done would make everyone confused. – tsh Nov 13 '20 at 01:34
  • Editing Questions to improve them (e.g. clarification, adding additional information, etc.) *is encouraged*. However, editing a Question to change it into a different question, which invalidates one or more answers, is against policy on Stack Overflow. Your edit here did so. The policy is that other users with edit privileges should proactively revert such changes. I have reverted your edit. You *are encouraged to [ask a new Question](/questions/ask)*, perhaps with a link to this one for additional context. We want to help, but your new/additional issue needs to be a new Question. – Makyen Nov 13 '20 at 02:15

1 Answers1

1

Try it Please,

userInput = input("First String: ")

with open("file1.txt", "r") as nameDb:
    fcon = nameDb.readline()

if userInput == fcon:
    print("Matched")
else:
    print("NO match")
Humayun Ahmad Rajib
  • 1,502
  • 1
  • 10
  • 22