Writing a simple function using while

A Python HOMEWORK Assignment asks me to write a function "that takes a positive integer as its value and displays a multiplication table that displays all the multiplications of the numbers before and including the input number." (Also using 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 I don’t know what to do next. I just need help with the algorithm. Please, NOT THE CORRECT CORRECT CODE , because I want to study. Instead, tell me logic and reasoning. Here are my thoughts:

  • The function must multiply all real numbers by the given value (n) times 1 less than n or (n-1)
  • The function must multiply all real numbers by n (including n) two times less than n or (n-2)
  • The function should multiply all real numbers by n (including n) three times less than n or (n-3) and so on ... until we reach n
  • When a function reaches n, the function must also multiply all real numbers by n (including n) times n
  • Then the function should stop or in a while "break" loop
  • Then the function should print the results.

So here is what I still have:

 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: This is what I got 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!" 

This is not yet complete! 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?

+8
python
source share
3 answers

I'm a little angry about requiring a while because Python loops better for loops. But learning is learning!

Think about it. Why While True ? It will never stop without a breakthrough statement, which I think is a little lame. What about another condition?

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

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

Your logic is pretty good. But here is my resume:

stop the cycle when the product of two numbers is n * n .

At the same time, print each number and its product. If the first number is not equal to n, increase it. After that, n start to increase the second. (This can be done with if statements, but nested loops would be better.) If they are both n, the while block will break because the condition will be met.

According to your comment, here is a little snippet of hued-y psuedocode:

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

where should the original assignment i and j be performed? Something, something else, and something funny?

+4
source share

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

+2
source share

The first step towards creating a specific 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 to the right of = must be trivial to determine, since we can calculate it by multiplying the other two numbers by each line; whose receipt is the core of the assignment. Think of two operands * as two counters, call them i and j . We can see that i calculates from 1 to 3 , but for each i j calculates from 1 to 3 (the result is only 9 lines; there will be n 2 lines). Therefore, you can try using nested loops, one for loop i ( 1 to n ), and the other for loop j ( 1 to n ) for each i . At each iteration of the nested loop, you can print a line containing i , j and i*j in the desired format.

+2
source share

All Articles