Python2.7:循环时出错,需要建议吗

Python2.7:循环时出错,需要建议吗,python,python-2.7,while-loop,Python,Python 2.7,While Loop,我在Python2.7中的while循环中遇到了一个小问题 我定义了一个过程,print\u multiplication\u table,它将一个正整数作为输入,并打印出一个乘法表,该表显示了所有整数乘法,包括输入数 这是我的打印乘法表函数: def print_multiplication_table(n): count = 1 count2 = 1 result = count * count2 print 'New number: ' + str(n)

我在Python2.7中的
while
循环中遇到了一个小问题

我定义了一个过程,
print\u multiplication\u table
,它将一个正整数作为输入,并打印出一个乘法表,该表显示了所有整数乘法,包括输入数

这是我的
打印乘法表
函数:

def print_multiplication_table(n):
    count =  1
    count2 = 1
    result = count * count2
    print 'New number: ' + str(n) 
    while count != n and count2 != n:
        result = count * count2
        print str(count) + " * " + str(count2) + " = " + str(result)
        if count2 == n:
            count += 1
            count2 = 1
        else:
            count2 += 1
以下是预期的输出:

>>>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
在添加
while
循环之前,一切正常:

while count != n and count2 != n:
现在我的输出如下所示:

>>>print_multiplication_table(2)
New number: 2
1 * 1 = 1
>>>print_multiplication_table(3)
New number: 3
1 * 1 = 1
1 * 2 = 2
我做错了什么?我该如何纠正

谢谢。

将while循环更改为:

while count将while循环更改为:

计数时尝试

在迭代工作的地方,你不会经常看到while。 这段代码在编辑器窗口中看起来很棒…..

试试看

import itertools
def print_multiplication_table(n):
    nums = range(1,n+1)
    operands = itertools.product(nums,nums)
    for a,b in operands:
        print '%s * %s = %s' % (a,b,a*b)

print_multiplication_table(3)
在迭代工作的地方,你不会经常看到while。 这段代码在编辑器窗口中看起来很棒

import itertools
def print_multiplication_table(n):
    nums = range(1,n+1)
    operands = itertools.product(nums,nums)
    for a,b in operands:
        print '%s * %s = %s' % (a,b,a*b)

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
range
生成单个操作数
product
生成笛卡尔乘积,
%
是将值代入字符串的运算符

n+1
是范围如何工作的人工制品。执行
帮助(范围)
查看说明

一般来说,在python中,最好使用一组丰富的特性来构造序列来创建正确的序列,然后使用一个相对简单的循环来处理这样生成的数据。即使循环体需要复杂的处理,如果您首先注意生成正确的序列,也会更简单

我还要补充一点,
,而当有一个确定的序列需要迭代时,
是错误的


我想通过推广上述代码来说明这是一种更好的方法。您的代码将很难做到这一点:

import itertools
import operator
def print_multiplication_table(n,dimensions=2):
    operands = itertools.product(*((range(1,n+1),)*dimensions))
    template = ' * '.join(('%s',)*dimensions)+' = %s'
    for nums in operands:
        print template % (nums + (reduce(operator.mul,nums),))
(此处为表意文字:)

您的代码需要为每个维度引入一个变量,这意味着需要一个列表或dict来跟踪这些值(因为您无法动态创建变量),并需要一个内部循环来处理每个列表项

给出:

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
range
生成单个操作数
product
生成笛卡尔乘积,
%
是将值代入字符串的运算符

n+1
是范围如何工作的人工制品。执行
帮助(范围)
查看说明

一般来说,在python中,最好使用一组丰富的特性来构造序列来创建正确的序列,然后使用一个相对简单的循环来处理这样生成的数据。即使循环体需要复杂的处理,如果您首先注意生成正确的序列,也会更简单

我还要补充一点,
,而当有一个确定的序列需要迭代时,
是错误的


我想通过推广上述代码来说明这是一种更好的方法。您的代码将很难做到这一点:

import itertools
import operator
def print_multiplication_table(n,dimensions=2):
    operands = itertools.product(*((range(1,n+1),)*dimensions))
    template = ' * '.join(('%s',)*dimensions)+' = %s'
    for nums in operands:
        print template % (nums + (reduce(operator.mul,nums),))
(此处为表意文字:)


您的代码需要为每个维度引入一个变量,这意味着需要一个列表或dict来跟踪这些值(因为您无法动态创建变量),并需要一个内部循环来对每个列表项进行操作。

它可以工作,但不会打印出“2*2=4”和“3*3=9”@MichaelVayvala。好的,让我想想。我已经在17分钟后发布了我的答案ago@MichaelVayvala. 是的,但要将问题标记为已解决,你必须接受答案。勾选此答案旁边的箭头,将其标记为已接受。虽然这确实解决了眼前的问题,但使用while循环伪造嵌套for循环才是真正的问题。它可以工作,但无法打印出“2*2=4”和“3*3=9”@MichaelVayvala。好的,让我想想。我已经在17分钟后发布了我的答案ago@MichaelVayvala. 是的,但要将问题标记为已解决,你必须接受答案。勾选此答案旁边的箭头,将其标记为已接受。虽然这确实解决了直接的问题,但使用while循环假装嵌套for循环才是真正的问题。