请问有会用Python编程语言编几个小程序的吗?谢谢题目如下。

发布网友 发布时间:2022-04-23 03:53

我来回答

1个回答

热心网友 时间:2022-05-11 05:06

题目1

if __name__ == '__main__':
    number = input("Please input a number:")
    number = int(number)
                                                                                
    i = 1
    sum = 0
    output = ""
    while i <= number:
        sum = sum + i
        if i == 1:
            output = "1"
        else:
            output = "%s+%d"%(output, i)
        i = i + 1
    print("Result: %s=%d"%(output, sum))

运行结果


题目2

if __name__ == '__main__':
    number = input("Please input a number:")
    number = int(number)
                                                
    i = 1
    while i <= number:
        sum = 0
        j = 1
        output = ""
        while j <= i:
            sum = sum + j
            if j == 1:
                output = "1"
            else:
                output = "%s+%d"%(output, j)
            j = j + 1
                                            
        print("%s=%d"%(output, sum))
        i = i + 1

运行结果

题目3

if __name__ == '__main__':
    number = input("Please input a number:")
    number = int(number)
                                     
    i = 1
    while i <= number:
        num = 0
        j = 1
        while j <= i:
            num = num * 10 + j
            j=j+1
                                     
        sum = num*8 + i
        print("%d*8+%d=%d"%(num,i, sum))
        i = i + 1

运行结果

题目4

if __name__ == '__main__':
    number = input("Please input a number:")
    number = int(number)
                          
    i = 1
    while i <= number:
        num = 0
        j = 1
        while j <= i:
            num = num * 10 + j
            j=j+1
                          
        sum = num*9 + (i+1)
        print("%d*9+%d=%d"%(num,i+1, sum))
        i = i + 1

运行结果

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com