Adding a raw_input argument to enhance 6/19?


def cube(number):
number = raw_input(“Please input a number to be cubed:”)
return number ** 3

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

return False

print cube(number)

I’ve been playing with the code from 6/19 to see if there is a way to implement an external input, but to no avail. The example above was my last attempt before seeking your help. There must be a way to do it, however my knowledge is limited. Any help would be greatly appreciated!



number = raw_input(“Please input a number to be cubed:”)
return number ** 3

Hi :smiley:

number will be a string here, because raw_input() returns a string. I tested your code and got this error:

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

(meaning ** doesn’t work with a string and an integer). they both have to be integers. You can convert variables to integer type using int().

(Spoiler )number = int(raw_input(“Please input a number to be cubed:”))


*…Also, when you test your code using print cube(number) :

NameError: name 'number' is not defined

(meaning there’s no variable called number. The number in your functions only exists inside those functions and not outside.)

You can do print cube(3) or print cube(5) etc.

Or you can add

number = 5
print cube(number)

Hope this helps…

I am happy to see you experimenting :blush:


Why would we have a function that takes a number then turn around and replace that input? It makes no sense at all when we really think about it.

The function has a specific purpose, return the cube of a number passed into it.

3 goes in => 27 comes out

This is what’s known as a pure function and there is no need to improve upon it in any way. It does exactly what it is supposed to do.

The function we should be calling is by_three() and if you wanted to have user input, then it would be before the call to that function.

user = raw_input('Enter any integer: ')
print (by_three(user))


Which point I did not stress but it is worth stressing. Python cannot do math on strings.

print (by_three(int(raw_input('Enter any integer: '))))

This will raise an exception if the input cannot be converted to an integer.


Okay, this makes much more sense! I didn’t realize raw_input() would be handled as a string. I appreciate your help. I’ll keep at it!


Thank you very much! This helps me greatly.

I appreciate the help!


You’re welcome!

At first I don’t think any of us did. We find out in short order, though, eh?

Consider the console in JavaScript. You can bring it up on your browser through the Tools or Developer menu. In Chrome,


entered in the location bar will open an empty window. Ctrl + Shift + J will open the JavaScript console. The > is the command prompt. Type or paste there and Enter to execute immediately or in the case of a function, store it in memory.

When we type,

 > 3 * 4
<- 12

we can see the immediate response. What we don’t see is what goes on in the background.

We wrote to the console what looks like numbers and operator, but what is stored in the input buffer is just characters. In other words, a string. Since the console is a JavaScript object, then it must have a method to examine this string and parse out the numbers and operator to evaluate the math and log the result.

Okay, that’s not Python, but we can witness an example of the behavior in our browser.

Back on Python, raw_input() (which is Python 2, and deprecated in Python 3) can be thought to operate along the same lines. It’s a function that polls the input buffer and when a character 13 is found, returns the content of the buffer, as a string. This might not be accurate but that’s not really important, It’s the general premise we’re looking at, not the technical details.

Later on you will learn more about exceptions and why they are invaluable and something we never wish to subvert, but rather give full rein and address the concerns that surface. As mentioned above, if the input cannot be cast to a number (integer) it will raise an exception…

Enter any integer: two
Traceback (most recent call last):
  File "<pyshell#178>", line 1, in <module>
    print (by_three(int(raw_input('Enter any integer: '))))
ValueError: invalid literal for int() with base 10: 'two'

The exception it raised is ValueError. 'two' is not a valid argument for int().


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