-4

I decided to make a program that would square a number just for fun. Using an online compiler, I entered my code and from what I saw there were no errors; it wouldn't run it would just have a blank console entry.

My code:

import math

def square():
    number = raw_input("Please enter a number for me to square.")  
    number*number  
    print "Your answer is..."  
    print number  

Repl.it output:

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Galakras
  • 3
  • 1
  • 3
    You have to **call** your function first. Then you will see that you have to assign your calculation to a variable (possibly `number`). – fredtantini Dec 02 '14 at 16:02
  • You don't need the `import math` (in addition to the answer Martijn provided) – Andy Dec 02 '14 at 16:11

1 Answers1

4

Do make sure you also call your function:

def square():
    # your function body here

square()

But in your function, you are ignoring the result of your calculation here:

number*number

Assign that result to something:

answer = number * number
print "Your answer is..."  
print answer  

You don't have a number, however. raw_input() returns a string, so you want to convert that to a number first:

number = int(number)

This assumes that the user actually entered something that can be converted to an integer; digits only, plus perhaps some whitespace and a + or - at the start. If you wanted to handle user errors gracefully here, take a look at Asking the user for input until they give a valid response for more advanced options.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I did the things mentioned but when I get it running it asks for the number but after I input a number and press enter I get this error, Traceback (most recent call last): File "", line 8, in File "", line 5, in square TypeError: can't multiply sequence by non-int of type 'str' – Galakras Dec 02 '14 at 16:07
  • @Galakras: updated; `raw_input()` returns a string, not an integer number. – Martijn Pieters Dec 02 '14 at 16:09