具有参数的基本功能

时间:2019-01-07 11:37:57

标签: python python-2.7 function user-input

我是Python的新手,并且是函数的绝对入门者。我尝试搜索答案,但我没有找到它,尽管我确定它是微不足道的。我想定义一个函数,将参数作为输入。然后,我想调用该函数,用户可以在其中定义参数并运行它。

基本上我想通过用户输入获得回文。因此,如果该函数被调用,例如回文(d),我们得到了abcdcba的印刷品。

我已经具有使用来自用户的raw_input行的功能。但是我希望用户将函数称为回文(字母),例如回文(g)。

def palindrome():
    input = raw_input("Till what letter would you like to have the palindrome: ")
    n = ord(input)
    for i in range(n - 97):
    print(chr(97 + i)),
    for j in range(n - 96):
        print(chr(n - j)),

palindrome()

我尝试了

def palindrome('a'):
    n = ord(a)
    for i in range(n - 97):
        print(chr(97 + i)),
    for j in range(n - 96):
        print(chr(n - j)),

 palindrome(c)

但这不起作用。

1 个答案:

答案 0 :(得分:2)

引号位置错误。

该功能应声明为

def palindrome(a):

因为a是将在函数内使用的变量的名称,而不是字符'a'

并且应该使用

调用

palindrome('c')

带引号的文本表示字符串文字:它是引号中字符的内容,而不是变量的标识符。