Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
def reverse(text):
    reversed_string = ("")
    for letter in range(0, (len(text)), -1):
        reversed_string += str(letter)

print(str(reversed_string))



When I print this, it says reversed_string is not a defined variable.

What I have tried:

I have tried using string methods such as str(), I have tried deleting the variable and making a different, I have tried pretty much everything.
Posted
Updated 27-Oct-22 12:52pm

1 solution

In this case the variable reversed_string is only known within the bounds of the reverse function. You need call reverse with a string as an argument e.g.
Python
print( reverse("Hello World!") )

But in order for that to work you'll need return reversed_string before exiting the reverse function.

Then you will have to figure out why your reverse function returns None.
 
Share this answer
 
Comments
Naveen Krishna1 31-Oct-22 16:55pm    
Sorry, I'm stupid. I'm trying to make this code without using the reverse/reversed function and without using [::-1]
k5054 31-Oct-22 17:37pm    
In this case the reverse() function would be the function you supplied a def for. Suppose instead of def reverse(text): you had declared def myreverse(text):, then you would call print( myreverse("Hello world!") to get the output you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900