Python for循环中的逻辑错误是什么?
问题是: 使用调试工具IDLE Debugger来更正 Python程序。程序应该生成一个模式的行 偶数列中的星号和奇数列中的美元,其中数字 行和列的数量由用户输入。预期产出为: 那么错误在哪里呢?我想不出是什么逻辑错误。 代码是:Python for循环中的逻辑错误是什么?,python,Python,问题是: 使用调试工具IDLE Debugger来更正 Python程序。程序应该生成一个模式的行 偶数列中的星号和奇数列中的美元,其中数字 行和列的数量由用户输入。预期产出为: 那么错误在哪里呢?我想不出是什么逻辑错误。 代码是: row = col = int(input("Enter number of row and column: ")) for row in range(row): for col in range(col): if row % 2 == 0
row = col = int(input("Enter number of row and column: "))
for row in range(row):
for col in range(col):
if row % 2 == 0 and col % 2 == 1:
print('*', end='')
elif row % 2 == 1 and col % 2 == 0:
print('$', end='')
else:
print(' ', end='')
print()
对于循环步骤的输入和变量,都使用col和row 由于
范围
(以及您的算法)是基于零的,在第一次运行的内部后,列
的值将比原始所需输入值小1。等等
这将有助于:
rows = columns = int(input("Enter number of row and column: "))
for row in range(rows):
for col in range(columns):
if row % 2 == 0 and col % 2 == 1:
print('*', end='')
elif row % 2 == 1 and col % 2 == 0:
print('$', end='')
else:
print(' ', end='')
print()
在python中,循环变量是在循环运行的范围内定义的,而不是在循环的内部范围内定义的。因此,它们都会“跳过”先前定义的变量,并在循环结束后保持定义状态。对于输入和循环步骤的变量,都使用col和row
由于范围
(以及您的算法)是基于零的,在第一次运行的内部后,列
的值将比原始所需输入值小1。等等
这将有助于:
rows = columns = int(input("Enter number of row and column: "))
for row in range(rows):
for col in range(columns):
if row % 2 == 0 and col % 2 == 1:
print('*', end='')
elif row % 2 == 1 and col % 2 == 0:
print('$', end='')
else:
print(' ', end='')
print()
在python中,循环变量是在循环运行的范围内定义的,而不是在循环的内部范围内定义的。因此,它们都会“跳过”先前定义的变量,并在循环结束后保持定义状态。您应该将用户输入的数字存储在不同的变量中,而不是用于迭代的变量中。
这起到了作用:
num = int(input("Enter number of row and column: "))
for row in range(num):
for col in range(num):
if row % 2 == 0 and col % 2 == 1:
print('*', end='')
elif row % 2 == 1 and col % 2 == 0:
print('$', end='')
else:
print(' ', end='')
print()
您应该将用户输入的数字存储在与用于迭代的变量不同的变量中。
这起到了作用:
num = int(input("Enter number of row and column: "))
for row in range(num):
for col in range(num):
if row % 2 == 0 and col % 2 == 1:
print('*', end='')
elif row % 2 == 1 and col % 2 == 0:
print('$', end='')
else:
print(' ', end='')
print()
作为一般规则:太多的逻辑条件会导致调试困难,太多的除法会导致代码速度慢(每个循环最多执行4次除法运算!),并且for
循环是嵌套的,因此您已经计算了所需迭代次数的平方
在循环开始之前,尽可能多地列出您的逻辑,这样可以简化事情
rows = cols = int(input("Enter number of row and column: "))
isAsterix = True
extraColumns = cols % 2 # 1 if odd; 0 otherwise
numDoubleColumns = int(cols / 2) # rounds down
for _ in range(rows):
if isAsterix:
bigCol = ' *' # will be multiplied up to column full width
pad = ' ' # used if odd number of columns
else:
bigCol = '$ ' # will be multiplied up to column full width
pad = '$' # used if odd number of columns
# yes you can multiply and add strings together...
print(bigCol*numDoubleColumns + pad*extraColumns)
isAsterix = not isAsterix # print the other symbol next time
对于这个例子,我很感激速度的提高可以忽略不计,但是记住可以删除for
循环的地方仍然是一个很好的实践。简化您的if
语句绝对是一件好事。作为一般规则:太多的逻辑条件会导致调试困难,太多的除法会导致代码缓慢(每个循环最多执行4次除法运算!)您的for
循环是嵌套的,因此您已经计算了所需迭代次数的平方
在循环开始之前,尽可能多地列出您的逻辑,这样可以简化事情
rows = cols = int(input("Enter number of row and column: "))
isAsterix = True
extraColumns = cols % 2 # 1 if odd; 0 otherwise
numDoubleColumns = int(cols / 2) # rounds down
for _ in range(rows):
if isAsterix:
bigCol = ' *' # will be multiplied up to column full width
pad = ' ' # used if odd number of columns
else:
bigCol = '$ ' # will be multiplied up to column full width
pad = '$' # used if odd number of columns
# yes you can multiply and add strings together...
print(bigCol*numDoubleColumns + pad*extraColumns)
isAsterix = not isAsterix # print the other symbol next time
对于这个例子,我很感激速度的提高可以忽略不计,但是记住可以删除for
循环的地方仍然是一个很好的实践。简化您的if
语句绝对是件好事。您是否尝试过使用建议的调试器来查看哪里出了问题?@user2357112ya。但我还是不明白。我试图编辑,但它没有显示预期的输出您的输出有什么问题?请向我们展示您的输出。amotsg的答案是write。您应该将内部循环变量的名称更改为其他名称。例如:对于范围内的col\u var(col):
@Navid777我知道了!感谢您的帮助:)您是否尝试使用建议的调试器来查看哪里出了问题?@user2357112ya。但我还是不明白。我试图编辑,但它没有显示预期的输出您的输出有什么问题?请向我们展示您的输出。amotsg的答案是write。您应该将内部循环变量的名称更改为其他名称。例如:对于范围内的col\u var(col):
@Navid777我知道了!感谢您的帮助:)@jornsharpe这是一个错误,在每次运行内部循环之后,在每次迭代外部循环时,它将从较小的范围开始。因为col的最终值将始终小于1。感谢您的帮助!:)@jonrsharpe这是一个错误,在每次运行内部循环之后,在每次迭代外部循环时,它都会从一个较小的范围开始。因为col的最终值将始终小于1。感谢您的帮助!:)我明白了!谢谢。谢谢你的解释:)我明白了!谢谢。谢谢你的解释:)