格式正确的乘法表 [英] Properly formatted multiplication table

查看:193
本文介绍了格式正确的乘法表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么会作出这样有组织的成整齐的表乘法表?我现在的code是:

How would I make a multiplication table that's organized into a neat table? My current code is:

n=int(input('Please enter a positive integer between 1 and 15: '))
for row in range(1,n+1):
    for col in range(1,n+1):
        print(row*col)
    print()

这正确相乘的一切,但有它以列表形式。我知道我需要它的巢和空间正常,但我不知道哪里去了?

This correctly multiplies everything but has it in list form. I know I need to nest it and space properly, but I'm not sure where that goes?

推荐答案

快捷方式(可能太多的横向空间虽然):

Quick way (Probably too much horizontal space though):

n=int(input('Please enter a positive integer between 1 and 15: '))
for row in range(1,n+1):
    for col in range(1,n+1):
        print(row*col, end="\t")
    print()

更好的办法:

Better way:

n=int(input('Please enter a positive integer between 1 and 15: '))
for row in range(1,n+1):
    print(*("{:3}".format(row*col) for col in range(1, n+1)))

这篇关于格式正确的乘法表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆