Beginnger Question

Mark McEahern marklists at mceahern.com
Tue Apr 9 10:33:16 EDT 2002


[Dennis]
> My current problem is this:
>
> x = 2
>
> def wrong(num):
>     return num + 1
>
> print wrong(x)
> print wrong(x)
>
> I cannot figure out why this (simplified) program won't increment x
> and produce 3 and 4.  This is probably indicative of a bigger problem
> with understanding functions (and their limitations).  Are there any
> sources out there that explain functions more in-depth and clearly?!
> Thanks for any and all help.

You are not assigning the return value to x, that's why x is unchanged.

Consider:

x = wrong(x)
x = wrong(x)
print x

By the way, even for exercises like this where you're exploring the
language, it's helpful to use names that indicate the intention of the code:

def addOne(num):
	return num + 1

// mark






More information about the Python-list mailing list