day4 循环结构

day4

循环结构

"""
循环语句
for in循环
"""
#1-100的和
sum= 0
for x in range(101):
    """
    range(101)//产生0到100的整数序列
    range(1,100)//产生1到99的整数序列
    range(1,100,2)//产生1到99的基数序列,步长为2
    range(a,b,step)//产生a到b的序列,含a不含b,其中step为每个数的间隔增量
    """
    sum+=x
print(sum)

#1-100的偶数和
sum = 0
for x in range(2,101,2):
    sum+=x
print(sum)
sum = 0
for x in range(1,101):
    if x%2==0:
        sum+=x
print(sum)

#while循环
#猜数字 1-100
import random
answer = random.randint(1,100)#产生1-100的随机数
count = 0
while True:
    count+=1
    number =int(input("please guess a number:"))
    if(number<answer):
        print("smaller tahn answer")
    elif(number>answer):
        print("big than answer")
    else:
        print("you are right")
        break
print("你总共猜了%d次"% count)
_____________________________________________________________
"""
输出乘法口诀表
"""
for i in range(1,10):
    for j in range(1,10):
        print("%d * %d = %d "%(i,j,i*j),end ="\t")
    print("\n")

"""
判断一个数是不是素数
"""
from math import sqrt
number = int(input("give a number:"))
end =int(sqrt(number))
prime = True
for i in range(1,end+1):
    if(number%i == 0):
        prime =False
        break;
if prime and number!=1:
    print("%d is prime"%number)
else:
    print("%d is not prime"%number)

_______________________________

"""
打印三角图案
*
**
***
****
    *
   **
  ***
*****
     *
    ***
   *****
  *******
  
 *********
"""
row = int(input("enter the raw"))
for i in range(row):
    for j in range(0,i):
        print("*",end =" ")
    print()
row = int(input("enter the raw"))
for i in range(row):
          for _ in range(i+1):
                  print("*",end = " ")
          print()


for i in range(row):
    for j in range(row):
        if j<row-i-1:
            print(" ",end ="")#先打印空格
        else:
            print("*",end="")#后打印*
    print()


raw = int(input("enter thr raw:"))
for i in range(raw):
    for j in range(raw-i-1):
            print(" ",end =" ")#前面rraw-i+1个空格
    for k in range(2*i+1):
        print("*",end =" ")#接下来打印*号
    print( )
上一篇:团队项目-第一阶段冲刺-day4


下一篇:Day4:编译型和解释型,idea的使用和配置