i cannot get print with this cod:

def reverse(text):
word = “”
l = len(text) - 1
while l >= 0:
word = word + text[l]
l -= 1
return word
print reverse(python)

thats make eror: File “python”, line 9, in


? please clarify/proof-read your question


you can check now, my problem is with print, i cannot see result of my code


Keep in mind that indentation isn’t optional in python, and other things may get lost too, so always make sure to tell the forum not to touch your code

def reverse(text):
     word = ""
     l = len(text) - 1
     while l >= 0:
         word = word + text[l]
         l -= 1
     return word
print reverse(python)

The full error message should look like this:

Traceback (most recent call last):                                             
  File "betacoder27529.py", line 8, in <module>
    print reverse(python)
NameError: name 'python' is not defined

It tells you where the problem is, and also what the problem is!

If you look at the 8th line of the code, you are indeed using the name python without having defined it


yes you explain problem good, but whats solution now ?
for get print


I don’t understand what’s stopping you, seems like something you can do yourself, and if not, you need to figure out what knowledge you’re missing.

Start by deciding what exactly you mean to do, you say “get print” but you haven’t said what you want to print

For example, I might want to print the result of 5 + 2, I would therefore compute 5 + 2 and then print the result:

print 5 + 2

Sorry to be so pedantic, but truly, it’s up to you to solve problems. You can ask others for help, but then you have to be the one leading with questions - not asking for a solution to what you want


my purpose is this when i want print a reverse with use function , thats dont work,

> i want convert abc to cbc

by use def reverse above
but when i use print thats dont work and make eror same yourself said.


You’d need to first create the value to reverse. What value type does python use for text and how can you create such a value?

If you type abc in code, then you’re telling python to look up the variable named abc. abc has nothing to do with what variables are in the program, it’s just text.
Same with python, you hadn’t created such a variable. You’d need a value which represents the letters p-y-t-h-o-n

a = 5
b = 7
a + b  # this is ok
a + c  # error, c undefined


i try in print statement input between quote mark " , and resolve
for now i can use:
print def reverse(“abc”)
to reverse that to cba and print that,
i think couse that for reverse is for string only


abc reversed in cba, not cbc

the def-keyword is only used when creating a function, not when calling it

but yes, the way to include text in code is to use string literals, which is regular text enclosed in quotes, and everything inside the quotes is just data, not code. This is very similar to writing 5 or 7, but no special quoting is needed because they are not valid variable names, they are just numbers

Several built-in value types have what is called value literals
Numbers are written directly, floats end with a dot and decimals, lists are surrounded by [] with commas separating values, dicts are {key: value}, sets are {value, value, value} and so on, there are a couple more.
Other times values may come from outside the program (network, user input, etc) or be generated by the program rather than written literally as part of the code


yes, thats my wrong, reverse to cba
thanks for your best :wink:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.