Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Why is my numeric variable not dividing?

So, am on the first video of the second section of the 'Python Basics' course entitled 'Naming Things', and have managed to somehow managed to fail at simply typing along with Kenneth... :S

There's a bit where he says that you can use numeric variables to divide, and then demonstrates, but I'm fairly sure I've done exactly the same thing, and it's given me a type error. But I checked, and the variable I'm trying to divide by 6 is definitely 42... Can anyone tell me where I've gone wrong?

Here's the code:

treehouse:~/workspace$ python                                                                                                                 
Python 3.5.0 (default, Nov 28 2017, 19:33:11)                                                                                                 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux                                                                                              
Type "help", "copyright", "credits" or "license" for more information.                                                                        
>>> favourite_number = "42"                                                                                                                   
>>> print (favourite_number)                                                                                                                  
42                                                                                                                                            
>>> favourite_number / 6                                                                                                                      
Traceback (most recent call last):                                                                                                            
  File "<stdin>", line 1, in <module>                                                                                                         
TypeError: unsupported operand type(s) for /: 'str' and 'int'  

Also, don't know if this would affect it, have tried both ways, but while I'm here, I wanted to ask about the fact that Kenneth appears to be putting spaces between each side of the variable and the "=" sign, or between "print" and the parenthesis enclosing the variable. Does this matter? Is it a standard stylistic thing that I should emulate?

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your variable favorite_number is the string literal "42". Try instead, favorite_number = 42. Also, the way Kenneth spaces things are standard Python style rules which you will learn quite a deal about later on. For now, try to emulate them as closely as you can.

Hope this helps! :sparkles:

Your favourite_number variable is being declared as a string because of the quotations

favourite_number = "42"

Try changing it to

favourite_number = 42

I believe this should work for ya.

Amazing, thanks guys! Helped me with the next puzzle as well, much appreciated! :)