8

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?

4

3 回答 3

4

我对while循环要求有点生气,因为for循环在 Python 中更适合这个。但是学习就是学习!

让我们想想。为什么要做一个While True?如果没有 break 语句,那将永远不会终止,我认为这有点蹩脚。还有一个条件呢?

变量呢?我想你可能需要两个。每个要相乘的数字一个。并确保在while循环中添加它们。

如果您需要更多帮助,我很乐意添加到此答案中。

你的逻辑很不错。但这里是我的一个总结:

当 2 个数字的乘积为 时停止循环n * n

同时,打印每个数字及其产品。如果第一个数字不是 n,则增加它。一旦那是n,开始增加第二个。(这可以使用 if 语句来完成,但嵌套循环会更好。)如果它们都是 n,则while块将中断,因为条件将被满足。

根据您的评论,这里有一小段提示性伪代码:

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

i 和 j 的原始分配应该去哪里?什么是东西、别的东西和有趣的东西?

于 2013-08-23T00:19:09.600 回答
2

由于您有两个计数器,因此使用嵌套循环可以更好地解决此问题。首先找出两个计数器的限制(开始值、结束值)。在函数开始时将计数器初始化为下限,并在 while 循环中测试上限。

于 2013-08-23T00:19:30.237 回答
2

能够产生特定输出的第一步是识别该输出中的模式。

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

右边的数字=应该很容易确定,因为我们可以通过将每行的其他两个数字相乘来计算它;获得这些是任务的核心。将 的两个操作数*视为两个计数器,我们称它们为iand j。我们可以看到i1to计数3,但是对于每个 i,从toj计数(总共有 9 行;通常会有 n 2行)。因此,您可以尝试使用嵌套循环,一个循环(从到)和另一个循环(从到)13i1nj1ni. 在嵌套循环的每次迭代中,您可以以所需的格式打印包含i,j的字符串。i*j

于 2013-08-23T00:21:41.783 回答