What’s wrong with this print line = ('Hello,' + full_name.title() + '!')?

Asked

Viewed 96 times

0

inserir a descrição da imagem aqui

I’m new to programming and I’m following Eric Matthes' book exercises. In the text editor, I typed print = ('Hello,' + full_name.title() + '!') and an error message appeared.

  • And what was the error message?

  • This here: print = 'Hello,' + full_name.title() + '!' Syntaxerror: invalid syntax

  • Is it python3? Or is it Python 2?

  • 1

    It seems to me that it is running on Python 2, not on 3, so, in fact, it is wrong. The problem is on = after the print. In Python 2, the print is a language statement and does not allow attribution. The correct is print 'Hello', nay print = 'Hello'.

  • is Python 2.7.15. I wonder if this is it?

  • Well, in this case I will search how I update python and try to run the program again. I warn you what happened. Thank you.

Show 1 more comment

2 answers

1


In python 2 print is a language statement, not an object, or is to use the equal operator = doing with print as if it were a variable will cause error:

Traceback (most recent call last):
  File "python", line 14
    print = 'Hello,' + full_name.title() + '!'
          ^
SyntaxError: invalid syntax

See that the ^ points the equals sign to be the syntax error, ie if you want to use this as variable then use a name other than a "function" or reserved word, create an intuitive var, for example:

msg = 'Hello,' + full_name.title() + '!'

Now if the intention is to display, just remove the signal from = and use as used on the other line:

print full_name

print 'Hello,' + full_name.title() + '!'

0

print if it is a language statement, the error is happening because when you use the assignment operator (=) trying to attribute something to print, the interpreter understands that this is a syntax error (SyntaxError).

Maybe you want something like:

name = "ada lovelace"

print( name.title() )
print( name.upper() )
print( name.lower() )

first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name

print( full_name )

print( "Hello, " + full_name.title() + "!" )

Exit:

Ada Lovelace
ADA LOVELACE
ada lovelace
ada lovelace
Hello, Ada Lovelace!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.