[SOLVED] How to fix this function? Results in Error

Issue

I tried adding a function to this code, it is supposed to display a the inputted sentence in reverse and I thought I had no errors but it is always resulting in error.

def reversed(text):
    # display the sentence in reverse 
    for i in reversed(range(len(text))): 
        print(text[i],end="") 
    print() 

text=input("Enter a sentence\n") 

while True: 
    # display the title 
    print() 
    print() 

    print("The Word Food Processor") 
    print("=======================") 
    print() 


    # display the menu 
    print("Choose an option:") 
    print("1 - display sentence in reverse order") 


    # error trap user's choice 
    while True: 

        choice=int(input("Enter choice: ")) 
        if (choice>=1 and choice <=6): 
            break 
        print("That is not a valid choice") 

    if (choice==1):
       reversed(text)

It is always ending in the following error. I am trying to use the function but it won’t work in a function. I had it out of one and it worked, but my teacher wanted for it to be a in function.

Traceback (most recent call last):
 File "main.py", line 47, <module> reversed(text)
 File "main.py", line 12, <module> reversed
  for i in reversed(range(len(text))):
 File "main.py", line 12, <module> reversed
  for i in reversed(range(len(text))):
 File "main.py", line 12, <module> reversed
  for i in reversed(range(len(text))):
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded in comparison

Solution

Currently you’re experiencing conflicting function names. There is a built in function of Python, reversed, which takes a container and reverses it’s contents.

You use this function in your custom made function, also named reversed, but Python thinks you’re trying to call your custom made function instead of the built in one.

To solve, name your custom function something else.

Answered By – csjh

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *