'Square a number with functions in python
This is an extremely easy question for Python. It's very basic Python as I'm still a beginner... to take a number, use a function and square it:
import math
nmb = int(raw_input("Enter a number to be squared: "))
def square(nmb):
y = math.pow(nmb,2)
return y
print str(nmb) + " squared is equal to " + str(square)
I've jiggered it around a few times, but the end result always prints something like "5 squared is equal to function square at 0x02BC87B0" instead of the result
I feel like I'm missing something really obvious, as my understanding of functions is still quite basic, but any pointers would set me on my way!
Solution 1:[1]
You are passing the function square
, not the return value of a call to square
, to str
. You want this:
print str(nmb) + " squared is equal to " + str(square(nmb))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | chepner |