-2

#The code is supposed to check if the user name typed in a text file. The text file includes

Username

John

The text file is (Pdata)

f = open("Pdata.txt")
liner = f.readlines()
user = str(liner[1])
print(liner[1])
tf = True
while tf:
          print("hello")
          userr = str(input("Pls enter username"))
          if userr is user:
                    
                    tf = False
tf = False

3 Answers3

0

change the while like this:

while tf == True:
    print("hello")
    userr = str(input("Pls enter username"))
    if userr == user:
        tf = False
bilakos
  • 145
  • 7
0

The problem is that you used 'is' operator in your if statement. What 'is' checks is if the two values are the same object, not if they have the same value. Since 'userr' is not the same object as 'user' it returns False, so the loop never ends.

Instead I would suggest using == operator as it check the values:

if userr == user:
    tf = False
  
user14678216
  • 2,886
  • 2
  • 16
  • 37
Ismail Hafeez
  • 730
  • 3
  • 10
0

When you do userr is user this implies that you are checking whether they are the same object which is not and hence the comparison fails. What you need is the == operator.

Also, you need to strip the \n character after reading the name.

f = open("Pdata.txt")
liner = f.readlines()
user = str(liner[1].rstrip())                                                                                           tf = True
while tf:
      print("hello", user)
      userr = str(input("Pls enter username"))
      if userr == user:
          tf = False
tf = False
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35