为什么我的代码会打印出“built-in method”和一些十六进制数?

7

这是我的关键函数:

def Key(message, decision):
    key = input("Input the key which will be used to encode the message.\n".lower)
    n = 0
    for i in range(len(key)):
        if 64 < ord(key[n]) < 91:
            raise ValueError(key[n], "is a capital letter!")
        else:
            n = n+1
    Keycode(decision, message, key)

当我调用它并输入消息后按回车键,会出现以下内容:

str对象的内置lower方法在0x0150E0D0处

有什么问题吗?我该如何解决?

6
看起来你在代码中的 somestring.lower 后面忘记加括号了。 - timgeb
5个回答

17
.upper.lower 后必须有一对括号。你可以在括号中输入自定义参数,但如果只想将输入的内容大写,请将括号留空。

例如:

user=(input("Enter a letter:")).upper()

这将更改为大写字母。


1
这就是我长期使用 Ruby 的后果。我刚刚花了30分钟来调试它。 - Sabrina Leggett
谢谢。我使用的是value.title而不是value.title()。 - yasbars

3
你需要在 lower 后使用封闭的括号。
key = input("Input the key which will be used to encode the message.\n".lower())

1
key = input("Input the key which will be used to encode the message.\n".lower)

由于lower函数缺少括号,请在函数调用后加上括号。因此语法应该像这样:key = input("输入用于编码消息的密钥。\n".lower())

1
"Key"包含这个有问题的行:

key = input("Now, input the key which will be used to encode the message.\n".lower)

当你(大概)想要传递字符串并将lower应用于input返回的内容时,它将字符串的lower方法作为输入传递给input


-2

尝试将你的工作保存在你的工作空间中。如果你正在使用Python提示符来检查你的结果,使用exit()命令并再次打开Python提示符。然后尝试像之前那样调用之前的函数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接