Python 2.7: Wrong while loop, need an advice -
i have small problem while
loop in python 2.7.
i have defined procedure, print_multiplication_table
, takes input positive whole number, , prints out multiplication, table showing whole number multiplications , including input number.
here print_multiplication_table
function:
def print_multiplication_table(n): count = 1 count2 = 1 result = count * count2 print 'new number: ' + str(n) while count != n , count2 != n: result = count * count2 print str(count) + " * " + str(count2) + " = " + str(result) if count2 == n: count += 1 count2 = 1 else: count2 += 1
here expecting output:
>>>print_multiplication_table(2) new number: 2 1 * 1 = 1 1 * 2 = 2 2 * 1 = 2 2 * 2 = 4 >>>print_multiplication_table(3) new number: 3 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
everything works fine until add while
loop:
while count != n , count2 != n:
and output looks this:
>>>print_multiplication_table(2) new number: 2 1 * 1 = 1 >>>print_multiplication_table(3) new number: 3 1 * 1 = 1 1 * 2 = 2
what have made wrong , how can fix that?
thanks.
change while loop to:
while count <= n , count2 <= n:
Comments
Post a Comment