Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a simple function using while

Tags:

python

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:

  1. The function should multiply all real numbers to the given value(n) times 1 less than n or (n-1)
  2. The function should multiply all real numbers to n(including n) times two less than n or (n-2)
  3. 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
  4. When the function reaches n, the function should also multiply all real numbers to n(including n) times n
  5. The function should then stop or in the while loop "break"
  6. 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!"

Edit: Here's what I have after all the wonderful answers from everyone

"""
i * j = answer
i is counting from 1 to n
for each i, j is counting from 1 to n 
"""


def print_multiplication_table(n): # n for a number
    if n >=0:
        i = 0
        j = 0 
        while i <n:
            i = i + 1
            while j <i:
                j = j + 1
            answer = i * j
            print i, " * ",j,"=",answer

    else:
        return "The input is not a positive whole number.Try another input!"

It's still not completely done! For example:

print_multiplication_table(2)
# The output 
>>>1  *  1 = 1
>>>2  *  2 = 4

And NOT

>>> 1 * 1 = 1
>>> 1 * 2 = 2
>>> 2 * 1 = 2
>>> 2 * 2 = 4 

What am I doing wrong?

like image 329
Andy_A̷n̷d̷y̷ Avatar asked Aug 23 '13 00:08

Andy_A̷n̷d̷y̷


3 Answers

I'm a little mad about the while loop requirement, because for loops are better suited for this in Python. But learning is learning!

Let's think. Why do a While True? That will never terminate without a break statement, which I think is kind of lame. How about another condition?

What about variables? I think you might need two. One for each number you want to multiply. And make sure you add to them in the while loop.

I'm happy to add to this answer if you need more help.

Your logic is pretty good. But here's a summary of mine:

stop the loop when the product of the 2 numbers is n * n.

In the mean time, print each number and their product. If the first number isn't n, increment it. Once that's n, start incrementing the second one. (This could be done with if statements, but nested loops would be better.) If they're both n, the while block will break because the condition will be met.

As per your comment, here's a little piece of hint-y psuedocode:

while something:
    while something else:
        do something fun
        j += 1
    i += 1

where should original assignment of i and j go? What is something, something else, and something fun?

like image 104
vroomfondel Avatar answered Sep 29 '22 21:09

vroomfondel


This problem is better implemented using nested loops since you have two counters. First figure out the limits (start, end values) for the two counters. Initialize your counters to lower limits at the beginning of the function, and test the upper limits in the while loops.

like image 33
perreal Avatar answered Sep 29 '22 22:09

perreal


The first step towards being able to produce a certain output is to recognize the pattern in that output.

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

The number on the right of = should be trivial to determine, since we can calculate it by multiplying the other two numbers on each row; obtaining those is the core of the assignment. Think of the two operands of * as two counters, let's call them i and j. We can see that i is counting from 1 to 3, but for each i, j is counting from 1 to 3 (resulting in a total of 9 rows; more generally there will be n2 rows). Therefore, you might try using nested loops, one to loop i (from 1 to n) and another to loop j (from 1 to n) for each i. On each iteration of the nested loop, you can print the string containing i, j and i*j in the desired format.

like image 21
arshajii Avatar answered Sep 29 '22 22:09

arshajii