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

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Another basic doubt, help!


    Here are two codes of mine to print the table of the entered number:
    Code:
    # The first one using a for loop
    def table(num):
    	for multiple in range(1,11):
    		print(str(num), '*', str(multiple), '=', str(int(num) * int(multiple)))
    # The second one using a while loop
    def table1(num):
    	multiple = 1
    	while multiple < 11:
    		print(str(num), '*', str(multiple), '=', str(int(num) * int(multiple)))
    		multiple += 1
    # Now, when I test whether the results shown by both the functions are true or not; I don’t get whether the values are true or not, but instead, get this result (input: table(8) == table1(9) ):
    8 * 1 = 8
    8 * 2 = 16
    8 * 3 = 24
    8 * 4 = 32
    8 * 5 = 40
    8 * 6 = 48
    8 * 7 = 56
    8 * 8 = 64
    8 * 9 = 72
    8 * 10 = 80
    9 * 1 = 9
    9 * 2 = 18
    9 * 3 = 27
    9 * 4 = 36
    9 * 5 = 45
    9 * 6 = 54
    9 * 7 = 63
    9 * 8 = 72
    9 * 9 = 81
    9 * 10 = 90
    Also, when the input is entered in the interactive prompt, I get all the values as same as when ran with a module file but at the end, I always get the result as True, no matter what!
    Now, here are my questions:
    1. Why does it print the result instead of True or False when run with a module file ( table(9) == table(8) ) and,
    2. Why does it always return True, even when the inputs are different in the interactive prompt? ( table(9) == table1(8) )
    3. Also, when I type in, ‘table(9), table(8)’ in the interactive prompt, I get two ‘None’s at the end. (I am not even using the print built-in function (3.0) anymore!) Why is that?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Please include _all_ of the source code, otherwise it's impossible to help.

    Also, you can use a for loop instead of while to make things shorter and sweeter.

    Code:
    def table2(num):
        for i in range(1,11):
            print("%s * %s = %s" % (num, i, str(num*i)))
    1. Why does it print the result instead of True or False when run with a module file ( table(9) == table(8) )
    It's because you don't explicitly return anything in your functions. Python by default returns None, so you're essentially comparing None to None, which is always true.
    http://www.catb.org/esr/faqs/smart-questions.html#code
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Code:
    '''
    1.	Why does it print the result instead of True or False when run with a module file ( table(9) == table(8) )
    
    Your functions print to stdout.  I expect them to display information.  You should, too.
    
    2.	Why does it always return True, even when the inputs are different in the interactive prompt? ( table(9) == table1(8) )
    
    Every python function returns a value.  Without an explicit return statement, functions return None.
    (None == None) is True
    
    3.	Also, when I type in, ‘table(9), table(8)’ in the interactive prompt, I get two ‘None’s at the end. (I am not even using the print built-in function (3.0) anymore!) Why is that?
    
    Same explanation as with 2.  You've made the tuple
    (None, None)
    Interactive python doesn't show None as the result of a computation.
    None is not the same as None,None .
    
    
    
    Now, if you want to compare the outputs of table1 and table, you could try
    '''
    
    
    import sys
    import io
    
    def out(num,multiple,sink):
        print(str(num), '*', str(multiple), '=', str(int(num) * int(multiple)), file=sink)
    
    # The first one using a for loop
    def table(num, sink=sys.stdout):
    	for multiple in range(1,11):
    		out(num,multiple,sink)
    
    # The second one using a while loop
    def table1(num, sink=sys.stdout):
    	multiple = 1
    	while multiple < 11:
    		out(num,multiple,sink)
    		multiple += 1
    
    t = io.StringIO()
    t1 = io.StringIO()
    table(4, t)
    table1(5, t1)
    
    t.seek(0)
    t1.seek(0)
    
    print('outputs of table(4) and table1(5) are the same?  {}'.format(t.read() == t1.read()))
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Code:
    '''
    1.	Why does it print the result instead of True or False when run with a module file ( table(9) == table(8) )
    
    Your functions print to stdout.  I expect them to display information.  You should, too.
    
    2.	Why does it always return True, even when the inputs are different in the interactive prompt? ( table(9) == table1(8) )
    
    Every python function returns a value.  Without an explicit return statement, functions return None.
    (None == None) is True
    
    3.	Also, when I type in, ‘table(9), table(8)’ in the interactive prompt, I get two ‘None’s at the end. (I am not even using the print built-in function (3.0) anymore!) Why is that?
    
    Same explanation as with 2.  You've made the tuple
    (None, None)
    Interactive python doesn't show None as the result of a computation.
    None is not the same as None,None .
    
    
    
    Now, if you want to compare the outputs of table1 and table, you could try
    '''
    
    
    import sys
    import io
    
    def out(num,multiple,sink):
        print(str(num), '*', str(multiple), '=', str(int(num) * int(multiple)), file=sink)
    
    # The first one using a for loop
    def table(num, sink=sys.stdout):
    	for multiple in range(1,11):
    		out(num,multiple,sink)
    
    # The second one using a while loop
    def table1(num, sink=sys.stdout):
    	multiple = 1
    	while multiple < 11:
    		out(num,multiple,sink)
    		multiple += 1
    
    t = io.StringIO()
    t1 = io.StringIO()
    table(4, t)
    table1(5, t1)
    
    t.seek(0)
    t1.seek(0)
    
    print('outputs of table(4) and table1(5) are the same?  {}'.format(t.read() == t1.read()))
    Life saver...thanks

IMN logo majestic logo threadwatch logo seochat tools logo