Mathbeginner
Mathbeginner

Reputation: 201

Very basic function with parameter

I'm very new to Python and an absolute beginner in functions. I tried to search for the answer but I haven't found it, although I'm sure it's trivial. I want to define a function with a parameter as input. Then I want to call the function, where the user can define the parameter, and run it.

Basically I want to get a palindrome with user input. So that if the function is called, e.g. palindrome(d), we get a print of abcdcba.

I already have the function working with a raw_input line from the user. But I want the user to call the function as palindrome(letter), so palindrome(g) for example.

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()

I tried

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)

But that doesn't work.

Upvotes: 1

Views: 39

Answers (1)

khelwood
khelwood

Reputation: 59112

You have the quotes in the wrong place.

The function should be declared as

def palindrome(a):

because a is a name for the variable that will be used within the function, not the character 'a'.

And the function should be called with

palindrome('c')

Quoted text indicates a string literal: it is the content of the characters within the quotes rather than an identifier for a variable.

Upvotes: 2

Related Questions