[Tutor] Problems understanding code output
Dave Angel
davea at davea.name
Thu Jul 11 00:46:56 CEST 2013
On 07/10/2013 05:05 PM, ska at luo.to wrote:
> def printMax(a, b):
> if a > b:
> print(a, 'is maximum')
> elif a == b:
> print(a, 'is equal to', b)
> else:
> print(b, 'is maximum')
> printMax(3, 4) # directly give literal values
> x = 5
> y = 7
> printMax(x, y) # give variables as arguments
>
>
>
> How the code above values to:
>
> 4 is maximum
> 7 is maximum
>
> and not to:
>
> 5 is maximum
> 7 is maximum
>
> This is going a little over my head, please advice, what am I missing in
> here?
>
You're calling the function twice, and two lines are printed out.
You're questioning why the first time it prints "5 is maximum."
The arguments to the function the first time are 3 and 4. The larger of
those is 4. How would it manage to come up with a 5 for that call?
Perhaps you'd see it easier if you temporarily added another print at
the beginning of printMax():
def printMax(a, b):
print("Comparing %d to %d" % a, b)
if a > b:
.......
--
DaveA
More information about the Tutor
mailing list