我如何保存返回作为变量的全范围python

发布于 2025-02-05 20:56:55 字数 236 浏览 1 评论 0原文

#first 
def table_(x):
    num=range(11)
    digit=int(input('Table of='))

    for num in range(11):
       # return(digit,'x',num,'=',num*digit)
        print(digit,'x',num,'=',num*digit)


num2=table_(2)
num2
#first 
def table_(x):
    num=range(11)
    digit=int(input('Table of='))

    for num in range(11):
       # return(digit,'x',num,'=',num*digit)
        print(digit,'x',num,'=',num*digit)


num2=table_(2)
num2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-02-12 20:56:55

例如,要打印从0到11的乘法表,您可以像这样修改您的函数:

def mul_table(digit):
    table = ''
    for num in range(12):
        table += f'{digit:2d} x {num:2d} = {num*digit:2d}\n'
    return table

用2的表测试:

table2 = mul_table(2)
print(table2)
 2 x  0 =  0
 2 x  1 =  2
 2 x  2 =  4
 2 x  3 =  6
 2 x  4 =  8
 2 x  5 = 10
 2 x  6 = 12
 2 x  7 = 14
 2 x  8 = 16
 2 x  9 = 18
 2 x 10 = 20
 2 x 11 = 22

To print the multiplication table from 0 to 11 for example, you can modify your function like this:

def mul_table(digit):
    table = ''
    for num in range(12):
        table += f'{digit:2d} x {num:2d} = {num*digit:2d}\n'
    return table

Test with table of 2:

table2 = mul_table(2)
print(table2)
 2 x  0 =  0
 2 x  1 =  2
 2 x  2 =  4
 2 x  3 =  6
 2 x  4 =  8
 2 x  5 = 10
 2 x  6 = 12
 2 x  7 = 14
 2 x  8 = 16
 2 x  9 = 18
 2 x 10 = 20
 2 x 11 = 22
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文