简体   繁体   中英

Why does my python program always show the opposite reaction

Ok, so I am making a file keeping software in python which will let users make, delete and upload files to a secret folder in the program.

Here is the part i am having trouble with:

user = input("User :")

if user is "Aymen":
     print("Welcome")

else:
     print("Access denied")

Why does my program regardless of the right input always show "Access denied"?

Don't use is to test for equivalence, use ==

is is a keyword to test if two values are the same exact instance in memory. But just because two things are equal does not mean that Python actually thinks they're the same object stored in one location. It's most commonly used with None but never in the way you're using it.

You should use

if user == "Aymen":

Use == instead of is for comparing strings. is tests for identity , not equality . That means Python simply compares the memory address a object resides in.

Example -

>>> s = input("Enter : ")
Enter : Aymen
>>> s is "Aymen"
False
>>> s == "Aymen"
True

If you want to compare to strings or variables, use "==", not "is". "is" is for testing, not comparing two variables or strings.

if user == "Aymen":
     print("Welcome")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM