Hello World Review Help

I am getting multiple errors while trying to add the (half_my_age) variable to the greeting. It works fine without the half my age part. Is there a rule where you can’t have to variables in a line?

my_age=30 half_my_age=(my_age/2) name= "Dan" greeting= "hello my name is "+name+'' "half my age is"+half_my_age" print(greeting)

File “script.py”, line 4
greeting= "hello my name is “+name+’’ “half my age is”+half_my_age”
SyntaxError: EOL while scanning string literal


No. No such rule. You do have a few syntax errors though. The string literals must begin and end with quotation marks either single or double quotes will do. You must concatenate the variables to the strings using the + operator. You must not put quotes before or after the variable name. If you do both, you’ll have a string literal of the variable name instead of a reference to the value the variable is assigned to. You also cannot use the + operator between instances of a string and a number, so the number must be converted to a string in order to concatenate it to a string. For example:

some_string = "Hello, World!" num = 1024 new_string = some_string + " My favorite number is " + str(num) + "." print(new_string)

An EOL ( End of Line ) error indicates that the Python interpreter expected a particular character or set of characters to have occurred in a specific line of code, but that those characters were not found before the end of the line . This results in Python stopping the program execution and throwing a syntax error .

The SyntaxError: EOL while scanning string literal error in python occurs when while scanning a string of a program the python hit the end of the line due to the following reasons:

  • Missing quotes
  • Strings spanning multiple lines

name= " Dan "
greeting= “hello my name is” +name+ “half my age is” + str(half_my_age)+


I still am not sure what I am doing wrong, The half_my_age part, I can do the math and assign it a variable, but I want it to do the math in the line.

I’m not sure what you mean. Do you mean that you want half_my_age calculated on the line where greeting is assigned? Like so?

my_age = 98
my_name = "Doyle"
greeting = "Hi my name is " + my_name + ". Half my age is " + str(my_age // 2) + "."
print(greeting) # prints-> Hi my name is Doyle. Half my age is 49.
1 Like

Yes, exactly. Except I already have the calculation assigned to a variable. I was able to get it to work another way

name= " Dan "
greeting= “hello my name is” +name+ “half my age is”

greeting2= half_my_age

print (greeting2)

I would like to understand why it wouldn’t work the first way though

Not sure I understand what you’re asking. When you print greeting, you should get:

hello my name is Dan half my age is

You don’t get the value that half_my_age is assigned to, because it isn’t part of string assigned:

Printing greeting2 by itself works fine. We can print numbers, but we can’t concatenate numbers to strings without converting the number to its string representation first.

1 Like

I need to go back in the lessons I suppose.

name= " Dan "

greeting= “hello my name is” +name+ “half my age is” str(half_my_age)

Still gives me an error. no + sign between the string and the number (half_my_age) is defined as (my age/2) in my head it should just do the math right there. And the variable half_my_age was converted to a string.

You still need the + to concatenate str(half_my_age) to the preceding string.
"some string " + 9 won’t work.
"some string " + str(9) does work because 9 is now the string "9" rather than a number.

Hint (if needed)

The math is already done on the line where half_my_age is assigned:

1 Like