SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Python Help: Can you please tell me what each line of this code is doing?

    Hello everyone,
    I am having trouble understanding this code: Specifically, what each line does.

    Here is what I think I know.

    1. Defines a function maximumValue with positional parameters.

    2. Assigns a variable to the value of x

    3. Uses the if structure and comparison operator to create a condition, to assign higher value or number to the maximum variable.

    4. Assign y(greater value) to the maximum variable.

    5. Uses the if structure and comparison operator to create a condition, to assign higher value or number to the maximum variable.

    6. Assign z(greater value) to the maximum variable.

    7. Return the maximum value or variable to the function.

    8. Get user input, convert to integer and assigned to variable a.

    9. Get user input, convert to integer and assigned to variable b.

    10. Get user input, convert to integer and assigned to variable c.

    11. Prints a message and the maximumValue function which arguments are assigned
    to the positional parameters.

    I am not sure I am getting this right, I really need a better explanation as to what is happening in each line.

    Practically, I understand it, theoretically, I am not sure.


    Code:
    def maximumValue( x, y, z ):
       maximum  =  x
       if  y  > maximum:
          maximum  =  y
    
       if  z  >  maximum:
          maximum  =  z
       return maximum
    a = int( raw_input( "Enter first integer: " ) )
    b = int( raw_input( "Enter second integer: " ) )
    c = int( raw_input( "Enter third integer: " ) )
    print "Maximum integer is:", maximumValue( a, b, c )
    Thanks everyone.

    Novice

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What the function in the code does is as follows:

    - first it checks if y > x
    - then checks if z > y

    The flaw however is that it only checks one way, it never bothers to check if x > y, and doesn't even at all compare y and z!

    There's a much simpler version of this that DOES do what's intended:

    Code Python:
    def maximum_value(*args):
        args = list(args)
        args.sort()
        args.reverse()
     
        return args[0]
     
    print maximum_value(1, 6, 3) # will print 6
    print maximum_value(129, 934, 4, 284, 1) # will print 934

    What this does is it takes an infinite number of parameters and will sort whatever is inside the parameters ascending, then reverses it (resulting in descending order) and obviously returning the first node will return the biggest value from the parameters

    Also keep in mind that the function naming convention is function_name rather than functionName


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •