-1

I'm supposed to write a statement that calls the recursive function backwards_alphabet() with input starting_letter. I've figured out how to do that. The problem is that at the end of my code it prints None. How do I get it to stop doing that?

(Anything above starting_letter = input() cannot be edited or added on to. I've tried and the site I'm using won't let me)

Here is my code

def backwards_alphabet(curr_letter):
    if curr_letter == 'a':
        print(curr_letter)
    else:
        print(curr_letter)
        prev_letter = chr(ord(curr_letter) - 1)
        backwards_alphabet(prev_letter)

starting_letter = input()

print(backwards_alphabet(starting_letter))

The expected output is supposed to be f e d c b a

My output is f e d c b a None

All I had to do was get rid of print

3 Answers3

1

Your code is correct, your problem is:

print(backwards_alphabet(starting_letter))

You're trying to print the function, which returns None as there is no return statement in the function.


Your code can also be simplified:

def backwards_alphabet(curr_letter: str):
    if curr_letter != '`':
        print(curr_letter)
        backwards_alphabet(chr(ord(curr_letter) - 1))

When you have to write a recursive function, always start by thinking what will the base condition be?

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
0

You can modify your code by replacing the print statements with a list that stores the letters and then return that list and it should work fine.

def backwards_alphabet(curr_letter):
    if curr_letter == 'a':
        return [curr_letter]
    else:
        letters = [curr_letter]
        prev_letter = chr(ord(curr_letter) - 1)
        letters += backwards_alphabet(prev_letter)
        return letters

starting_letter = input()

result = backwards_alphabet(starting_letter)
print(' '.join(result))
Ake
  • 805
  • 1
  • 3
  • 14
-1

Always handle exceptions:

def backwards_alphabet(curr_letter):
        if curr_letter == 'a':
                return [curr_letter]
        else:
                letters = [curr_letter]
                prev_letter = chr(ord(curr_letter) - 1)
                letters += backwards_alphabet(prev_letter)
                return letters
print( "Enter any ALPHABET from a-z: ", end="")
while True:
        starting_letter = input()
        if len(starting_letter) == 1:
                if starting_letter.isalpha():
                        break
                else:
                        print("PROVIDE ANY ALPHABET FROM a-z\n", end="")
                        print( "Enter any ALPHABET from a-z: ", end="")
        else:
                if not starting_letter.isalpha():
                        print("PROVIDE ANY ALPHABET FROM a-z\n", end="")
                        print( "Enter any ALPHABET from a-z: ", end="")
                else:
                        print("Provide only one ALPHABET as input.\n", end="")
                        print( "Enter any ALPHABET from a-z: ", end="")
print("Input: "+starting_letter)
result = backwards_alphabet(starting_letter)
print(' '.join(result))
</code>

@Tim Roberts
Thank you for your knowledge transfer.
I am from C/C++ @ SunOS/AIX/CYGWIN_NT/HP-UX/Linux.
Hence I handled that using C++/C format exceptions.
<code>
                if ( 1 == starting_letter.isalpha() ):
...
</code>
  • Don't write booleans that way. Do `if starting_letter.isalpha():` or `if not starting_letter.isalpha()`. And that's not handling exceptions, that's data validation. – Tim Roberts Jun 30 '23 at 05:43
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 01 '23 at 18:28