使用迭代打印出表 1 到 10 的函数
A function that prints out tables from 1 to 10 using iteration
def tablesOneToTen(): # a function that will print out multiplication tables from 1-10
x = 1
y = 1
while x <= 10 and y <= 12:
f = x * y
print(f)
y = y + 1
x = x + 1
tablesOneToTen()
我正在尝试创建一个函数,该函数将从 1-10
的乘法 table 中给出值。
除了嵌套 while
循环之外,我是否应该添加 if
和 elif
语句才能使此代码正常工作?
对于这类迭代任务,您最好使用 for
循环,因为您已经知道您正在使用的边界,还有 Python 使创建 for
循环变得特别容易。
在 while
循环中,您必须使用条件检查您是否在范围内,同时显式增加您的计数器,从而更有可能出错。
既然你知道你需要乘法表来计算 x
和 y
的值,范围从 1-10
你可以,为了让你熟悉循环,创建两个 for
循环:
def tablesOneToTen(): # a function that will print out multiplication tables from 1-10
# This will iterate with values for x in the range [1-10]
for x in range(1, 11):
# Print the value of x for reference
print("Table for {} * (1 - 10)".format(x))
# iterate for values of y in a range [1-10]
for y in range(1, 11):
# Print the result of the multiplication
print(x * y, end=" ")
# Print a new Line.
print()
运行 这将为您提供所需的表格:
Table for 1 * (1 - 10)
1 2 3 4 5 6 7 8 9 10
Table for 2 * (1 - 10)
2 4 6 8 10 12 14 16 18 20
Table for 3 * (1 - 10)
3 6 9 12 15 18 21 24 27 30
对于 while
循环,逻辑是相似的,但当然比它需要的更冗长,因为您必须初始化、评估条件和递增。
作为其丑陋的证明,while
循环看起来像这样:
def tablesOneToTen():
# initialize x counter
x = 1
# first condition
while x <= 10:
# print reference message
print("Table for {} * [1-10]".format(x))
# initialize y counter
y = 1
# second condition
while y <=10:
# print values
print(x*y, end=" ")
# increment y
y += 1
# print a new line
print(" ")
# increment x
x += 1
使用Python 3
for i in range(1, 10+1):
for j in range(i, (i*10)+1):
if (j % i == 0):
print(j, end="\t")
print()
或:
for i in range(1, 10+1):
for j in range(i, (i*10)+1, i):
print(j, end="\t")
print()
输出:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
希望它能帮助你得到 1 到 10 table。
a = [1,2,3,4,5,6,7,8,9,10]
for i in a:
print(*("{:3}" .format (i*col) for col in a))
print()
def tablesOneToTen(): # a function that will print out multiplication tables from 1-10
x = 1
y = 1
while x <= 10 and y <= 12:
f = x * y
print(f)
y = y + 1
x = x + 1
tablesOneToTen()
我正在尝试创建一个函数,该函数将从 1-10
的乘法 table 中给出值。
除了嵌套 while
循环之外,我是否应该添加 if
和 elif
语句才能使此代码正常工作?
对于这类迭代任务,您最好使用 for
循环,因为您已经知道您正在使用的边界,还有 Python 使创建 for
循环变得特别容易。
在 while
循环中,您必须使用条件检查您是否在范围内,同时显式增加您的计数器,从而更有可能出错。
既然你知道你需要乘法表来计算 x
和 y
的值,范围从 1-10
你可以,为了让你熟悉循环,创建两个 for
循环:
def tablesOneToTen(): # a function that will print out multiplication tables from 1-10
# This will iterate with values for x in the range [1-10]
for x in range(1, 11):
# Print the value of x for reference
print("Table for {} * (1 - 10)".format(x))
# iterate for values of y in a range [1-10]
for y in range(1, 11):
# Print the result of the multiplication
print(x * y, end=" ")
# Print a new Line.
print()
运行 这将为您提供所需的表格:
Table for 1 * (1 - 10)
1 2 3 4 5 6 7 8 9 10
Table for 2 * (1 - 10)
2 4 6 8 10 12 14 16 18 20
Table for 3 * (1 - 10)
3 6 9 12 15 18 21 24 27 30
对于 while
循环,逻辑是相似的,但当然比它需要的更冗长,因为您必须初始化、评估条件和递增。
作为其丑陋的证明,while
循环看起来像这样:
def tablesOneToTen():
# initialize x counter
x = 1
# first condition
while x <= 10:
# print reference message
print("Table for {} * [1-10]".format(x))
# initialize y counter
y = 1
# second condition
while y <=10:
# print values
print(x*y, end=" ")
# increment y
y += 1
# print a new line
print(" ")
# increment x
x += 1
使用Python 3
for i in range(1, 10+1):
for j in range(i, (i*10)+1):
if (j % i == 0):
print(j, end="\t")
print()
或:
for i in range(1, 10+1):
for j in range(i, (i*10)+1, i):
print(j, end="\t")
print()
输出:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
希望它能帮助你得到 1 到 10 table。
a = [1,2,3,4,5,6,7,8,9,10]
for i in a:
print(*("{:3}" .format (i*col) for col in a))
print()