1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
14
Rep Power
0

Okay, so I'm working through Codecademy's Python tutorials, having some trouble with this project on functions. This script is supposed to define cube(n) which takes n and raises it to the thrid power. Then it defines by_three(m) which is supposed to check to see if 'm' is evenly divisable by 3, if so, it calls the cube() on 'm', if not, it returns False. Code and error below.
Code:
```def cube(n):
"""Returns the cube root of a number"""
return n**3
def by_three(m):
"""Checks to see if number is divisable by 3, if so, it calls the cube() on that number, if not, it returns False"""
if m % 3 == 0:
cube(m)
else:
return False```
Code:
`Oops, try again. Your function by_three returns None on input 3 when it should return 27. by_three should return cube(n) if n is evenly divisible by 3.`
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
14
Rep Power
0

Solved

Code:
`      return cube(n)`
3. python defines some standard operations unusually. Here's a shorter version of by_three
Code:
```def cube(n):
"""Returns the cube root of a number"""
return n**3

def by_three(m):
"""Checks to see if number is divisable by 3, if so, it calls the cube() on that number, if not, it returns False"""
return (m % 3 == 0) and cube(m)

for i in range(8):
print(i, by_three(i))```
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
14
Rep Power
0

?

Code:
```def distance_from_zero(x):
xis = type(x)
if xis in set('int float'.split()):
return abs(x)
else:
return "Not an interger or float!"```
Code:
`Oops, try again. Your function seems to fail on input -10 when it returned 'Not an interger or float!' instead of '10'`
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
14
Rep Power
0
Code:
```def distance_from_zero(x):
if type(x) in [int,float]:
return abs(x)
else:
return "Not an integer or float!"```
using the simple if-in cleared it up. plus it was unnessisary to declair a variable for what the type(x) was, when I could simply check type(x).
So what then is the difference between using
Code:
`if type(x) in [int,float]:`
and
Code:
`if type(x) in set('int float'.split()):`
?
6. int and float are classes, while 'int' is a string object.

>>> int != 'int'
True
>>> print(str('int'))
int
>>> print(str(int))
<class 'int'>
>>>

And with long lists (length 4 or more) and more than one test of that set continue to use sets

if type(x) in set([int,float]):
Last edited by b49P23TIvg; February 19th, 2014 at 12:45 PM.
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
14
Rep Power
0
so using
Code:
`set([ ])`
indicates a list of classes, where as
Code:
`set('  '.slice())`
indicates string objects?
8. Say what? Numbers have directories?

set(' '.slice()) # indicates an error condition
Code:
```\$ python -c "'  '.slice()"
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'str' object has no attribute 'slice'```
strings don't have a slice method.

A python set is a collection of hashable objects. The set constructor takes an iterable object. The iterator returns these hashable objects from which python builds a set. Numbers, tuples, frozensets, and strings are common examples of hashable objects. These objects cannot change.

For example, the directory of 6.28 shows an __hash__ method. Tau
Code:
```\$ python -c 'print(dir(6.28))'
['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']```

Code:
```\$ python -c "print( set( (int, 83, 'float', 83, 83) ) )"
{83, <class 'int'>, 'float'}```
Last edited by b49P23TIvg; February 19th, 2014 at 04:05 PM.
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
14
Rep Power
0
Originally Posted by b49P23TIvg
set(' '.slice()) # indicates an error condition
Code:
```\$ python -c "'  '.slice()"
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'str' object has no attribute 'slice'```
strings don't have a slice method.

A python set is a collection of hashable objects. The set constructor takes an iterable object. The iterator returns these hashable objects from which python builds a set. Numbers, tuples, frozensets, and strings are common examples of hashable objects. These objects cannot change.

For example, the directory of 6.28 shows an __hash__ method. Tau
Code:
```\$ python -c 'print(dir(6.28))'
['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']```

Code:
```\$ python -c "print( set( (int, 83, 'float', 83, 83) ) )"
{83, <class 'int'>, 'float'}```
O_O umm... lol