如何根据用户输入的行和列 headers 创建乘法 table?
How to create a multiplication table based on user input with row and column headers?
需要使 range() 值基于用户输入的任何内容作为开始和结束因素。它还需要 headers 行和列。请使用简单的语法我是编程新手!
# This program creates a multiplication table with nested loops and limits the
# factors based on user input values.
#
# References:
# https://www.youtube.com/watch?v=0rWHH5JfKdo
# https://www.youtube.com/watch?v=Dpkfh-hX4-M
#
# statement-print-t-end/27312325
def nested_loop(start, end):
for i in range(start, end + 1):
for j in range(start, end + 1):
print(i * j, end='\t')
print('')
def get_value(label):
print("Enter " + label + " value:")
value = int(input())
return value
def main():
start = get_value("starting")
end = get_value("ending")
nested_loop(start, end)
main()
你的代码已经完成了大部分工作,它在用户指定的时间段内打印乘法 table 的二维矩阵,你只缺少 headers 可以添加的部分这个
def nested_loop(start, end):
# printing the top header
print('', end='\t')
for i in range(start, end + 1):
print(i, end='\t')
print('')
for i in range(start, end + 1):
print(i, end='\t') # printing the running column alongside the matrix
for j in range(start, end + 1):
print(i * j, end='\t')
print('')
def get_value(label):
print("Enter " + label + " value:")
value = int(input())
return value
def main():
start = get_value("starting")
end = get_value("ending")
nested_loop(start, end)
main()
使用像这样的输入
3, 10
程序打印出来:
3 4 5 6 7 8 9 10
3 9 12 15 18 21 24 27 30
4 12 16 20 24 28 32 36 40
5 15 20 25 30 35 40 45 50
6 18 24 30 36 42 48 54 60
7 21 28 35 42 49 56 63 70
8 24 32 40 48 56 64 72 80
9 27 36 45 54 63 72 81 90
10 30 40 50 60 70 80 90 100
同意已接受的答案,但只需要展示 python 恋人如何使事情复杂化...就像...这样:
def nasty_loop(start, end):
print('\t'+'\t'.join(str(x) for x in range(start,end+1)))
for y in range(start, end+1):
print("%d\t" % y + '\t'.join(str(x*y) for x in range(start,end+1)))
需要使 range() 值基于用户输入的任何内容作为开始和结束因素。它还需要 headers 行和列。请使用简单的语法我是编程新手!
# This program creates a multiplication table with nested loops and limits the
# factors based on user input values.
#
# References:
# https://www.youtube.com/watch?v=0rWHH5JfKdo
# https://www.youtube.com/watch?v=Dpkfh-hX4-M
#
# statement-print-t-end/27312325
def nested_loop(start, end):
for i in range(start, end + 1):
for j in range(start, end + 1):
print(i * j, end='\t')
print('')
def get_value(label):
print("Enter " + label + " value:")
value = int(input())
return value
def main():
start = get_value("starting")
end = get_value("ending")
nested_loop(start, end)
main()
你的代码已经完成了大部分工作,它在用户指定的时间段内打印乘法 table 的二维矩阵,你只缺少 headers 可以添加的部分这个
def nested_loop(start, end):
# printing the top header
print('', end='\t')
for i in range(start, end + 1):
print(i, end='\t')
print('')
for i in range(start, end + 1):
print(i, end='\t') # printing the running column alongside the matrix
for j in range(start, end + 1):
print(i * j, end='\t')
print('')
def get_value(label):
print("Enter " + label + " value:")
value = int(input())
return value
def main():
start = get_value("starting")
end = get_value("ending")
nested_loop(start, end)
main()
使用像这样的输入
3, 10
程序打印出来:
3 4 5 6 7 8 9 10
3 9 12 15 18 21 24 27 30
4 12 16 20 24 28 32 36 40
5 15 20 25 30 35 40 45 50
6 18 24 30 36 42 48 54 60
7 21 28 35 42 49 56 63 70
8 24 32 40 48 56 64 72 80
9 27 36 45 54 63 72 81 90
10 30 40 50 60 70 80 90 100
同意已接受的答案,但只需要展示 python 恋人如何使事情复杂化...就像...这样:
def nasty_loop(start, end):
print('\t'+'\t'.join(str(x) for x in range(start,end+1)))
for y in range(start, end+1):
print("%d\t" % y + '\t'.join(str(x*y) for x in range(start,end+1)))