反向单词的代码

时间:2018-07-13 14:40:00

标签: python

我这样做是为了反驳一个单词,但无法获得帮助。

while True:
    word = input("Normal Word: ")
    newWord = ""
    for i in range(len(word)):
        newWord += word[len(word)-(i+1)]
    print("Reversed Word: '" + newWord() + "'\n")

错误提示         “ TypeError:'str'对象不可调用”

1 个答案:

答案 0 :(得分:1)

您的代码的主要问题是:

在打印功能中调用newWord(),而不是简单地newWord


while True:
    word = input("Normal Word: ")
    newWord = ""
    for i in range(len(word)):
        newWord += word[len(word) - (i + 1)]
    print("Reversed Word: '" + newWord() + "'\n")

可能更Python化的解决方案是:

newWord = word[::-1]
print(newWord)