Python - 凯撒密码错误

时间:2014-11-11 13:06:27

标签: python

我试图在python中创建一个Caesar密码但是我遇到了一个错误。 我的代码是:

print("Enter desired function.")
print("1 - Encode")
print("2 - Decode")
function = input("")
if function == "1":
    print("How much do you want to offset it by?")
    offset = int(input(""))
elif function == "2":
    print("How much is it offset by?")
    offset = int(input("")) * -1
print("Enter the message.")
message = input("")
upperMessage = message.upper
length = len(message)
for i in range(length):
    letter = upperMessage[i-1]
    number = ord(letter)
    offsetNumber = number + offset
    if offsetNumber > 90:
        highOffset = offsetNumber - 90
        offsetNumber = 65 + highOffset
    if offsetNumber < 65:
        lowOfsset = 65 - offsetNumber
        offsetNumber = lowOffset + 90
    newLetter[i-1] = chr(offsetNumber)
 print(newLetter)

它开始运行良好但是当我输入消息时收到错误:

TypeError: 'builtin_function_or_method' object is not subscriptable

我试过更换线

letter = upperMessage[i-1]

letter = upperMessage(i-1)

但这会返回错误

TypeError: upper() takes no arguments (1 given)
你能帮帮我吗? 非常感谢, RFox

1 个答案:

答案 0 :(得分:0)

您需要添加括号以实际调用方法

upperMessage = message.upper()

否则,如果您熟悉C ++,则将函数本身分配给upperMessage,类似于(-ish)函数指针。