6. Practice Makes Perfect


#1

Hello everybody. Thanks for looking into the trouble I am going through.

https://www.codecademy.com/courses/python-beginner-c7VZg/1/5?curriculum_id=4f89dab3d788890003000096#

I am getting the following error messages on python console :

Type a number: 2
Traceback (most recent call last):
File “python”, line 13, in
File “python”, line 7, in by_three
TypeError: not all arguments converted during string formatting

I would like to receive either the False boolean result or the cube of number I inserted. Here is my code :

number = raw_input("Type a number: ")

def cube(number):
    return number**3

def by_three(number):
    if number % 3 == 0 :
        cube(number)
        return cube(number)
    else:
        return cube(False)

cube(by_three(number))
print ("The cube of %s is %s") % (number, cube(number))

Thanks in advance.


#2

by_three should simply return false if number isn’t divisible by 3, not the cube of false

also here:

cube(by_three(number))

what are you doing?


#3

Thanks for helping me. I have to invoke at some point the procedure I want to use, no? But I think I know what I did wrong, I should invoke “by_three” procedure only, since it invokes internally “cube” , correct?


#4

yep, exactly. And then when calling by_three should supply an actual integer to satisfy the number function parameter


#5

It is still giving me errors. I updated the code as follows:

number = raw_input("Type a number: ")

def cube(number):
    return number**3

def by_three(number):
    if number % 3 == 0 :
        cube(number)
        return cube(number)
    else:
        return False

by_three(number)
print ("The cube of %s is %s") % (number, by_three(number))

Which results in:

Type a number: 3
Traceback (most recent call last):
File “python”, line 13, in
File “python”, line 7, in by_three
TypeError: not all arguments converted during string formatting

What am I doing wrong ?


#6

raw_input stores the result as string, not as integer


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.