Question about examples format

Hello

I only started to code a few days ago and have been doing the python 3 course. I have a question that has followed me through some of the modules.

If i were to run the exact example i would get an error. Here is an example:

result = divide_by_four(16)
print("16 divided by 4 is " + str(result) + “!”)
result2 = divide_by_four(result)
print(str(result) + " divided by 4 is " + str(result2) + “!”)

And here is my code that worked:

def calculate_age(current_year, birth_year):
age = current_year - birth_year
return age

my_age = calculate_age (2049, 1993)
dads_age = calculate_age (2049, 1953)
print("I am “+str(my_age) +” years old " +“and my dad is “+str(dads_age) +” years old”)

As you can see, my code does not have spaces between "+str(my_age) and so on. I would like a clarification on whats actually correct and why?

Hope someone can help me understand this.
Best regards

It may be a bit late to edit (so perhaps add it as reply) but please format code when posting to the forums and include a link to the lesson if it’s relevant, the following FAQ covers the best way to set up your question (and covers formatting).

What error are you getting? I can’t see an issue at first glance but it’s very difficult to tell without formatting.

Oh okay, i’ll be sure to think about that next time.

This is the lesson i am referring to.
https://www.codecademy.com/courses/learn-python-3/lessons/intro-to-functions/exercises/returns

I will now try to format the code the correct way:


result = divide_by_four(16)
# result now holds 4
print("16 divided by 4 is " + str(result) + "!")
result2 = divide_by_four(result)
print(str(result) + " divided by 4 is " + str(result2) + "!")

This is my code which ran correctly :


def calculate_age(current_year, birth_year):
  age = current_year - birth_year
  return age

my_age = calculate_age (2049, 1993)
dads_age = calculate_age (2049, 1953)

print("I am "+str(my_age) +" years old " +"and my dad is " +str(dads_age) +" years old")

as you can see i did not leave a space between the plus sign and the str(dads_age) and it worked.
I was wondering if it always should be spaces between as the example suggests or if it does not matter?

I hope this makes more sense, you’ll have to excuse me. I’m very new to this.

1 Like

There are always two parts to this answer: What can the programming language handle and what are good or best practices?

You are right, python will cope fine with both (with and without space). But for consistency and readability I would do with spaces:

"I am " + str(my_age) + " years old" # rest of the string

actually, scratch all that. I would use .format():

https://pyformat.info/

or f-string when possible (python3.6+ I think, would have to check)

2 Likes

Ahh so it doesn’t need to an actual stack trace, that’s good. As @stetim94 mentioned the convention would be to add whitespace around operators.

Python’s style guide (which it warns is guidance, not rules) would follow that convention expect if removing the whitespace makes it easier to read when using multiple operators including those with different priorities (under “Other recommendations” in the link)-

1 Like

Thanks a lot for the clarification!