为什么如果我输入一个号码输入唯一的工作吗?

问题描述 投票:0回答:4

我已经成功地取得了蟒蛇的密码系统,但密码只能出数字,而不是字母。下面是它背后的编码:

    password = str(input("Please enter password to continue: "))
    if password == 'dog':
        print ("Welcome")
    else:
        print ("WRONG PASSWORD")

同时具有密码是一个整数,不工作这是行不通的。

编辑:很抱歉把错误的代码,新的这个网站。我现在已经添加引号“狗”,但现在给人的终端这个错误

Please enter password to continue: dog
Traceback (most recent call last):
  File "pass.py", line 1, in <module>
    password = str(input("Please enter password to continue: "))
  File "<string>", line 1, in <module>
NameError: name 'dog' is not defined

最后编辑:通过改变STR(输入到STR(固定的raw_input这是因为我用它使用Python 2,有谁知道如何使终端做巨蟒-3,而不是2终端?

python passwords python-2.x
4个回答
3
投票

您正在尝试一个string类型传递给integer,这是行不通的。您可以将字符串比较为int!

在Python字符串需要他们周围的语音标记("STRING")。如果没有语音标记,Python会假定它是一个整数或浮点数。

您正确的代码应该是:

password = str(input("Please enter password to continue: "))
if password == "dog":
    print ("Welcome")
else:
    print ("WRONG PASSWORD")

编辑:这似乎也正在使用Python 2中(因为你使用的终端)。在Python 2 input函数试图获得输入作为蟒表达,而不是作为一个字符串。尝试使用raw_input相反,如果你正在使用Python 2,这将让输入的字符串。

字符串的讲话标志着仍然适用。您的代码将如下所示:

password = str(raw_input("Please enter password to continue: "))
if password == "dog":
    print ("Welcome")
else:
    print ("WRONG PASSWORD")

0
投票

“类型”是这里的关键。

你铸造你从用户获得(这是一个字符串)int输入 - 整型:

3 == '3'

这是假的!一个字符串可以永远不等于一个整数。

我会建议不投它int,保持一个str,它应该只是罚款。


0
投票

为了让非经常性:

password="dog"
password1=input("Enter password?")
if password==password1:
    print("Welcome")
else:
    print("Incorrect password")

然而,使其rcurring你只是这样做:

condition = True
password="dog"
while condition:
    password1=input("Enter password?")
    if password==password1:
        print("Welcome")
    else:
        print("Incorrect password")

-2
投票

如果你正在使用python 2,请尝试使用:

inp = str(raw_input('Please enter password to continue: '))
print("Welcome") if password == "password_here" else print ("WRONG PASSWORD")
© www.soinside.com 2019 - 2024. All rights reserved.