0

I'm very new to python, but I found this strange: Code: I was typing in Sagemathcloud's sageworksheet file:

x=y+1
def f(x):
    return y
x=-1
print(x)
print(f(x))

As an output, I kept getting 7 or sometimes 49, something quite randomnumbers no matter what value of x I input. Any insights please?

P.S. I'm trying to make it look like a code, but I'm, not sure how to do that, for example, def was in the next lmine after I typed: x=y+1.

arshajii
  • 127,459
  • 24
  • 238
  • 287

1 Answers1

0

If that's the only code you have, then y isn't defined before it is used. y will take the value of what was previously in the memory space it's using - and that may not even be an int which causes the randomness of the numbers you're experiencing.

Alex Blundell
  • 2,084
  • 5
  • 22
  • 31