-2
def square(x):
    x = 2**x

    x = 2.0
    while x < 100.0:
    print x, '\t', square(x)
    x = square(x)

I tried to print. But it won't print. What I actually wanted were the squares of 2. But it won't be printing? What am I doing wrong?

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
praneet
  • 45
  • 6
  • 2
    `x = 2.0` is just going to reset x back to `2.0` each time you run the function. so you do `x = 2^x` and throw away that value. – Marc B Aug 22 '14 at 20:12
  • 1
    You have infinite recursion -- you're calling `square(x)` inside the definition of `square(x)`. – Barmar Aug 22 '14 at 20:13
  • You are making it an infinite recurrsion just return the value – Tushar Gupta Aug 22 '14 at 20:14
  • Remember that Python cares very much about indentation. All the statements starting with `x = 2.0` are part of the function definition, because they're indented. – Barmar Aug 22 '14 at 20:15
  • Can you fix your indentation first? are `x = 2.0` and `while ...` actually unindented? – solarc Aug 22 '14 at 20:18

2 Answers2

0

you should return a value.

def square(x):
    y = 2**x
    return y

and increment x value.

while x < 100.0:
     print x, '\t', square(x)
     x += increment_value

I think square(x) = x^2 not 2^x ?

0

You can even make it look like

while i<100.0:
   print x**2,"\t"
   i+=1#If you increment i by one

The mistake you made is not returning the value Add a statement return sqvalue where sqvalue is the squared of the value you give as input.

def square(int x):
   sqvalue=x**2
   return sqvalue

or write it in a single line function

def square(int x):
   return x**2

I suggest you may take a look at lambda(anonymous functions) for these simple tasks.