Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 24th, 2013, 11:04 AM
 Akshat1
Contributing User

Join Date: Feb 2013
Posts: 84
Time spent in forums: 20 h 18 sec
Reputation Power: 1
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
April 24th, 2013, 12:01 PM
 partoj
Contributing User

Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 12 h 26 m 47 sec
Reputation Power: 1
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)))```

Quote:
 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

#3
April 24th, 2013, 12:17 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,140
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 17 m 40 sec
Reputation Power: 455
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)

__________________
[code]Code tags[/code] are essential for python code!

#4
April 29th, 2013, 02:41 AM
 Akshat1
Contributing User

Join Date: Feb 2013
Posts: 84
Time spent in forums: 20 h 18 sec
Reputation Power: 1
Quote:
 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

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Another basic doubt, help!