#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Please help with test problem!!


    This does not successfully pass the test please, how do i make it pass without changing the test requirements?


    def compare(a, b):
    """
    >>> compare(3, 1)
    '3 is greater than 1'
    >>> compare(2, 5)
    '2 is less than 5'
    >>> compare(4, 4)
    '4 and 4 are equal'
    """
    str(a)
    str(b)
    if a > b:
    return a, "is greater than", b
    elif a < b:
    return a, "is less than", b
    elif a == b:
    return a, "and", b, "are equal"

    if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=True)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,714
    Rep Power
    480
    [QUOTE=matais]This does not successfully pass the test please, how do i make it pass without changing the test requirements?


    You're given numbers. Compare the numbers. Otherwise you'll get '10' < '2' which is stupid. Thankfully, you forgot to include assignments so these statements are useless:

    str(a)
    str(b)


    This is a tuple:
    (1,"a string",any_object,)

    tuples are immutable---great for sets and as dictionary keys.

    In many cases python let's you construct tuples without the parentheses. I usually write them but you might therefor describe me as verbose. Whatever. For example,

    x, y = y, x # swap the values of x and y

    In
    return a, 'is less than', b

    constructs the tuple (a, 'is less than', b) then returns that tuple. The tuple does not display like a string. Your doctests fail.


    Next you look up string formatting.

    Code:
    >>> str(1)+'X' # concatenation, note you'd be missing a space character to separate 1 from X
    '1X'
    
    >>> '%g X %g'%(a,b) # % string operator targeted for deprecation.  Right hand argument must be a tuple.
    
    >>> '{} X {}'.format(a,b) # possible future of string formatting.  Simplest to use in the most common case.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo