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!

1 Like
#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
1 Like
#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?

1 Like
#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

2 Likes
#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.

#11

Found comma(,) function was useful. Thanks @stetim94

#12

x = 1
y = 2
sum = x + y

print (sum)# I get 3 as expected

y = 3
print (sum)# Still get 3 while expecting 4

1 Like
#13

Sum is variable which is already assigned with the sum of x and , so if you change the value of y also , the value of sum will not change . if you really want to change the value of sum then you can use the below program.

x = 1
y = 2

sum = x+y
print sum
3

y = 3
sum = x+y
print sum
4

1 Like
#14

Remember that computer code is not algebra. the = symbol is not an “equals sign,” it is an assignment operator: it assigns the value on the right to the variable on the left, no more.

To do what you are thinking - to make sum continually update - you will need a sum() function: next lesson, I believe.

1 Like