FAQ: Learn Python: Syntax - Changing Numbers


#1

This community-built FAQ covers the “Changing Numbers” exercise from the lesson “Learn Python: Syntax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Changing Numbers

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

I tried to type this:

quilt_width = 8
quilt_length = 12
print("total squares needed:" + quilt_width * quilt_length )

To show the value in the same line as the text without using 2 prints.

And I got the following error:

Traceback (most recent call last):
  File "script.py", line 3, in <module>
    print("total squares needed:" + quilt_width * quilt_length )
TypeError: Can't convert 'int' object to str implicitly

I’m not sure what I did wrong. What is the correct syntax to achieve what I wanted?

Am I getting ahead of myself and this is something I will learn down the line?


#3

quilt_width * quilt_length gives an integer (96). Trying to concatenate str + integer gives errors, as you can see in the error:

TypeError: Can't convert 'int' object to str implicitly

#4

Can you explain more please?

What’s the correct way to write it then?

Is using two prints the only way?


#5

You use + to attempt string concatenation, but you don’t have two strings. Thus you get an error.

So either you will need to convert the integer to a string, so you can stick with your current approach (using +).

Or the must be another way to solve this, but i don’t know what you have learned. Have you learned string Formatting Operator? Or the string .format() method? Or the use of a comma?


#6

No, not yet.

I’m still at the very beginning of Python.

So far, I learner these things:

  • Comments
  • Print
  • Strings
  • Variables
  • Errors
  • Numbers
  • Calculations
  • Changing Numbers

I will google the error I got and try to understand what happened, you already gave me some hints.

I guess I still lack the basics, I will understand what I did wrong after few more lessons.

Thank you for your time.


#7

so you had string and numbers, that is good.

The problem is that using + to add number and string together, will result in a TypeError, you can add these two types together. these types don’t go together, just like certain things don’t go together.

you can use a comma (,) instead of a + to overcome your problem. I had no idea this exercises was so early in a course. Had i known, i would have simplified my answer. Sorry about that


#8

This makes more sense to me now as a newbie hahaha

Thanks, I will try your solution and Google to learn more about it.

Thanks again.


#9

6 days later, I understand the error I made.

I was trying to add a number to a string, like saying 8 + trees equals what? Which doesn’t make any sense logically.

I changed my syntax to since you mentioned a comma:

quilt_width = 8
quilt_length = 12
print("total squares needed:" , quilt_width * quilt_length )

And it worked just fine.