1

I am a python newbie, learning the basics now. I tried the following def function.But it indicates an error. Can somebody tell me where I make the mistakes in the syntax.

(I wrote it with the proper indentation. but the Stakoverflow window does not indicate as I paste it)

# This will test the reverse function of the program

def rev(text) :
    text = input()
    return text [::-1]

print("Please Enter a word")
input()
rev(text)

input() # This is to avoid the screen closing immediately after executing the
        # program

1 Answer 1

4

You have to assign a value to a variable before using it.

print("Please Enter a word")
input()
rev(text)

You are using text here, but you havn't assigned any value to it. You might want to do like this

print("Please Enter a word")
text = input()
rev(text)

Now, input will read data from the user and it will be available with text

Suggestion 1: As @seaotternerd suggested in the comments, you don't need to print the message and read inputs, you can directly do,

input("Please Enter a word") 
Sign up to request clarification or add additional context in comments.

2 Comments

It also isn't necessary to print the prompt and then call input() separately. input("Please enter a word") will accomplish both.
@seaotternerd Thanks :) Added that in the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.