【问题标题】:def function python - Help me with syntaxdef function python - 帮助我了解语法
【发布时间】:2013-12-21 10:20:20
【问题描述】:

我是 python 新手,现在学习基础知识。我尝试了以下 def 函数。但它表明错误。谁能告诉我我在哪里犯了语法错误。

(我用正确的缩进写了它。但我粘贴时Stakoverflow窗口没有显示)

# This will test the reverse function of the program

def rev(text) :
    text = input()
    return text [::-1]

print("Please Enter a word")
input()
rev(text)

input() # This is to avoid the screen closing immediately after executing the
        # program

【问题讨论】:

    标签: python python-3.x function


    【解决方案1】:

    在使用变量之前,您必须为其赋值。

    print("Please Enter a word")
    input()
    rev(text)
    

    您在此处使用text,但尚未为其分配任何值。你可能想这样做

    print("Please Enter a word")
    text = input()
    rev(text)
    

    现在,input 将从用户那里读取数据,并且可以使用 text

    建议1:正如@seaotternerd在cmets中所建议的,你不需要print消息和阅读输入,你可以直接做,

    input("Please Enter a word") 
    

    【讨论】:

    • 也不需要打印提示然后单独调用input()。 input("请输入一个单词") 两者都会完成。
    • @seaotternerd 谢谢 :) 在答案中添加了这一点 :)
    猜你喜欢
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 2018-08-11
    • 1970-01-01
    相关资源
    最近更新 更多