Thursday, 22 August 2013

Writing a simple function using while

Writing a simple function using while

A Python HOMEWORK Assignment asks me to write a function "that takes as
input a positive whole number, and prints out a multiplication, table
showing all the whole number multiplications up to and including the input
number."(Also using the while loop)
# This is an example of the output of the function
print_multiplication_table(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
I know how to start, but don't know what to do next. I just need some help
with the algorithm. Please DO NOT WRITE THE CORRECT CODE, because I want
to learn. Instead tell me the logic and reasoning. Here is my reasoning:

The function should multiply all real numbers to the given value(n) times
1 less than n or (n-1)
The function should multiply all real numbers to n(including n) times two
less than n or (n-2)
The function should multiply all real numbers to n(including n) times
three less than n or (n-3) and so on... until we reach n
When the function reaches n, the function should also multiply all real
numbers to n(including n) times n
The function should then stop or in the while loop "break"
Then the function has to print the results
So this is what I have so far:
def print_multiplication_table(n): # n for a number
if n >=0:
while somehting:
# The code rest of the code that I need help on
else:
return "The input is not a positive whole number.Try anohter input!"

No comments:

Post a Comment