简体   繁体   English

为什么我的python程序总是表现出相反的反应

[英]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. 好的,所以我在python中创建了一个文件保存软件,它允许用户创建,删除和上传文件到程序中的秘密文件夹。

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 is a keyword to test if two values are the same exact instance in memory. is是一个关键字,用于测试两个值是否与内存中的确切实例完全相同。 But just because two things are equal does not mean that Python actually thinks they're the same object stored in one location. 但仅仅因为两件事情是平等的并不意味着Python实际上认为它们是存储在一个位置的相同对象。 It's most commonly used with None but never in the way you're using it. 它最常用于None但绝对不会使用它。

You should use 你应该用

if user == "Aymen":

Use == instead of is for comparing strings. 使用==而不是is比较字符串。 is tests for identity , not equality . is 对身份的测试,而不是平等 That means Python simply compares the memory address a object resides in. 这意味着Python只是比较一个对象所在的内存地址。

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" is for testing, not comparing two variables or strings. “是”用于测试,而不是比较两个变量或字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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