【问题标题】:Python username and password creatorPython用户名和密码创建者
【发布时间】:2016-11-13 05:48:46
【问题描述】:

我正在 python 上创建一个程序,允许您创建用户名和密码,这是我目前所拥有的:

#Code
username = str(input("Please enter a username:"))
print("Your username is",username,",proceed?")
raw_input = input()
if raw_input() == 'no':
    re_u = input("Please re-enter username")
else:
    import getpass
    mypass = getpass.getpass("Please enter your password:")

我遇到的麻烦是: 回溯(最近一次通话最后): 第 6 行,在 如果 raw_input() == '否': TypeError: 'str' 对象不可调用。

请帮忙

【问题讨论】:

  • 你是用 python 2.7 还是 3.x 写的?

标签: python


【解决方案1】:

排队:

raw_input = input()

如果您使用的是 python 2.7,您将覆盖默认的 raw_input 并将其设为字符串。

如果您使用的是 python 3,您将创建一个字符串 raw_string,其值为 input()

因此,在这两种情况下,当您尝试像调用函数一样调用raw_input 时,您都会收到错误消息。

TypeError: 'str' 对象不可调用。

if raw_input() == 'no':

这里,raw_input 是一个字符串,而不是可调用的。你不能像函数一样调用它。

现在,工作代码应该如下所示:(python 2.7 解决方案)

import getpass

username = raw_input("Please enter a username:")
username_confirmation = raw_input("Your username is " + username + ",proceed?")
if username_confirmation == "No":
    username = raw_input("Please re-enter username")
else:
    password = getpass.getpass("Please enter your password:")

# do something with username and password

【讨论】:

  • 如果这是 Python 3,那么 raw_input 不再是默认方法,因为它被重命名为 input
  • @Makoto 我感觉这是 python 2,因为 OP 肯定是初学者并且错误地使用了raw_input()。如果这是python 3,他应该不熟悉。无论如何,我已经编辑了这两种情况的答案。
  • print 成为 Python 3 中的一个函数,input 的行为与 Python 2 中的 raw_input 相同(在 Python 2 中,input 也做了一些不同的事情)。我强烈怀疑 OP 使用的是 Python 2,但感谢您更正它。
【解决方案2】:

raw_input 是一个字符串,但您试图将其用作函数,并在其末尾添加 ()。改用这个:

if raw_input == 'no':

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-27
    • 2020-05-08
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    相关资源
    最近更新 更多