首页 > 编程语言 > 详细

python学习之基础语法循环语句

时间:2018-04-25 10:14:08      阅读:190      评论:0      收藏:0      [点我收藏+]
1.输入一个5位数字,判断其位数: val = input("please input a 1-99999 number:") vals = int(val) if vals >= 1000: if vals >=10000: print("This is 5") else: print("This is 4") else: if vals >= 100: print("This is 3") else: if vals >= 10: print("This is 2") else: print("this is 1")
2.输入一个数字打印乘法表:
A = input("please input you`r Num:")
NUM = int(A)
sum = 0
for i in range(1,NUM+1):
    for j in range(i,NUM+1):
        sum =i*j
        print(str(i) + "x" + str(j)+"=" + str(sum)+"\t",end=" ")
        #print(str(i) + "*" + str(j)+"=" + str(sum))
    print()       
please input you`r Num:9
1x1=1    1x2=2   1x3=3   1x4=4   1x5=5   1x6=6   1x7=7   1x8=8   1x9=9   
2x2=4    2x3=6   2x4=8   2x5=10  2x6=12  2x7=14  2x8=16  2x9=18  
3x3=9    3x4=12  3x5=15  3x6=18  3x7=21  3x8=24  3x9=27  
4x4=16   4x5=20  4x6=24  4x7=28  4x8=32  4x9=36  
5x5=25   5x6=30  5x7=35  5x8=40  5x9=45  
6x6=36   6x7=42  6x8=48  6x9=54  
7x7=49   7x8=56  7x9=63  
8x8=64   8x9=72  
9x9=81   
输入一个数字输出乘法表:
A = input("please input you`r Num:")
NUM = int(A)
sum = 0
for i in range(1,NUM+1):
    for j in range(1,i+1):
        sum =i*j
        print(str(j) + "*" + str(i)+"=" + str(sum)+"\t",end=" ")
        #print(str(i) + "*" + str(j)+"=" + str(sum))
    print()
please input you`r Num:9
1*1=1    
1*2=2    2*2=4   
1*3=3    2*3=6   3*3=9   
1*4=4    2*4=8   3*4=12  4*4=16  
1*5=5    2*5=10  3*5=15  4*5=20  5*5=25  
1*6=6    2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
1*7=7    2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
1*8=8    2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
1*9=9    2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  
3.输如一个数字求其阶乘之和:
如果输入3:和=1x1+1x1x2+1x1x2x3
n = int(input("please input you`r num:"))
a = 1
sum = 0
for i in range(1,n+1):
        for j in range(1,i+1):
            a *= j
            #print("--->a"+ "+"+str(a))
        sum += a
        a = 1
print(sum)
please input you`r num:3
9

python学习之基础语法循环语句

原文:http://blog.51cto.com/12013190/2107515

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!