创建九九乘法表?

问题描述 投票:0回答:6

我是编程初学者,正在练习如何使用嵌套 for 循环在 python 2.7.5 中制作乘法表。 这是我的代码

x=range(1,11)
y=range(1,11)
for i in x:
    for j in y:
        print i*j
    pass

嗯,结果是正确的,但它没有像我希望的那样以方阵形式出现。请帮助我改进代码

python python-2.7
6个回答
8
投票

打印时应不换行。

x = range(1,11)
y = range(1,11)
for i in x:
    for j in y:
        print i*j,    # will not break the line
    print   # will break the line

2
投票

您可以添加格式以保持单元格宽度恒定

x = range(1,11)
y = range(1,11)
for i in x:
    for j in y:
        # substitute value for brackets
        # force 4 characters, n stands for number
        print '{:4n}'.format(i*j),  # comma prevents line break
    print  # print empty line

1
投票

Python 的 print 语句默认将换行符添加到您希望在输出中包含的数字。我想您希望内部循环的尾随空格和外部循环末尾的换行符。

您可以通过使用来实现这一点

print i * j,   # note the comma at the end (!)

并在外循环块的末尾添加一个新行:

print ''

要了解有关尾随逗号及其工作原理的更多信息,请查看此处:“如何在 Python 中打印而不带换行符或空格?”。请注意,它在 Python 3 中的工作方式有所不同。

最终代码应如下所示:

x=range(1,11)
y=range(1,11)
for i in x:
    for j in y:
        print i*j,
    print ''

您还可以查找“ ”特殊字符,这将使您获得更好的格式(即使这个旧资源也足够好了:https://docs.python.org/2.0/ref/strings.html


0
投票

使用此代码。它的效果要好得多。我必须在学校做这个,我可以告诉你,在花了大约 4 个小时后,它完美地工作了。

def returnValue(int1, int2):
    return int1*int2

startingPoint = input("Hello! Please enter an integer: ")
endingPoint = input("Hello! Please enter a second integer: ")

int1 = int(startingPoint)
int2 = int(endingPoint)

spacing = "\t"

print("\n\n\n")
if int1 == int2:
    print("Your integers cannot be the same number. Try again. ")
    

if int1 > int2:
    print("The second number you entered has to be greater than the first. Try again. ")


for column in range(int1, int2+1, 1):  #list through the rows(top to bottom)
    
    if column == int1:
            for y in range(int1-1,int2+1):
                if y == int1-1:
                    print("", end=" \t") 
                    
                else: 
                    individualSpacing = len(str(returnValue(column, y)))
                    print(y, " ", end=" \t")
            print()
            
    print(column, end=spacing)

    
    for row in range(int1, int2+1, 1): #list through each row's value. (Go through the columns)
        #print("second range val: {:}".format(row))
        
            individualMultiple = returnValue(row, column)
            print(individualMultiple, " ", end = "\t")
        
    print("")

祝你有美好的一天。


0
投票
#Generate multiplication table by html


import random

from copy import deepcopy

N = 15



colors = ['F','E','D','C','B','A']

i = 0

colorsall = []

while i < N:

    colornow = deepcopy(colors)

    random.shuffle(colornow)

    colornow = "#"+"".join(colornow)

    colorsall.append(colornow)

    i += 1

t = ""

for i in range(1,N+1):

    s = ''

    for j in range(1,N+1):

        if j >= i:

            s += '<td style="background-color:' + colorsall[i-1] + '">'+str(i*j)+'</td>'

        else:

            s += '<td style="background-color:' + colorsall[j-1] + '">'+str(i*j)+'</td>'

    s = "<tr>" + s + "</tr>"



    t = t + s + '\n'

    

print('<table>' + t + '</table>')

取自https://todaymylearn.blogspot.com/2022/02/multiplication-table-in-html-with-python.html [披露:我的博客]


0
投票

#这是如何在 python 中打印乘法
对于范围 (1,6) 内的 i: 对于范围(1,6)中的数字: print(f"{num} * {i} = {i*num}",end=" ") 打印(“”) print(f"表 {i}") 打印(” ”)

© www.soinside.com 2019 - 2024. All rights reserved.