password = 'letmein'
user_input = str(input('Enter your password:\n'))
while user_input is not password:
a = str(input('Password is wrong, Please try again:\n'))
if a == password:
print('Welcome Back!')
break
else:
continue
-
1Use != instead of is not – bitflip Oct 14 '22 at 18:27
4 Answers
Don't use is not
to compare strings.
else: continue
is useless at the end of a loop.
Print "Welcome" after the loop.
input()
returns a string so no need to use str()
.
password = 'letmein'
while user_input := input("Enter your password: ") != password:
print("Password is wrong, Please try again:")
print('Welcome Back!')
See also: Why does comparing strings using either '==' or 'is' sometimes produce a different result?

- 13,291
- 5
- 35
- 66
In the loop header you're using the is
operator to see if two variables point to the same object, but in the if statement you're using the ==
operator to see if their values are the same. Since they're not the same object (you can use the id
function to check) you will enter the loop. Once inside the loop, if you do enter the same value, you'll get the "Welcome Back!" message.

- 398,270
- 210
- 566
- 880
as others explained above, here's a clean version of what you want to do
password = "letmein"
while True:
password = input("Enter your password: ")
if password == "letmein":
print("Welcome Back!")
break
else:
print("Password is wrong, Please try again:\n")
continue

- 659
- 4
- 17
The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not. The same is the case for != and is not.
whenever we use str() method, it creates a new object of class String.
If we will use while user_input != password:
While loop will not get executed for once.

- 72
- 4