第三课
1.for结合range
s=0
for i in range(1,101):
s+=i
print('1到100的和',s)
s=0
for i in range(1,101):
if i%2==0:
s-=i
else:
s+=i
print(s)
s=0
for i in range(1,11):
s+=i**2
print(s)
s=0
for i in range(1,101):
if i%3==0 and i%10==5:
s=s+1
print(s)
2.while 循环
'''
语法
while 条件判断
循环体代码快
解读
只要条件成立程序就一直再次循环直到循环结束
'''
'''
while 思想
1.重复做的操作是什么==打印数据 每次打印都是一个
2.做这些事情的条件==只要打印的数据没有到10 就得一直打印
'''
num=1
while num<=10:
print(num)
num+=1
print(num)
num=2
while num<=10:
print(num)
num+=2
c=0
num=1
while num<=100:
c+=num
num+=1
print(c)
c=0
i=1
while i<=100:
c+=(-1)**i*(-i)
i=i+1
print(c)
c=0
i=1
while i<=100:
if i%3==0 and i%10==5:
c+=1
i+=1
print(c)
p=1023
d=0
while p>0 :
p=p-(p//2+1)
d+=1
print(d)
num1=int(input('请输入一个整数'))
i=0
while num1>0 :
if num1%2==0:
num1=num1//2-1
else:
num1=(num1-1)//2
i+=1
print(i,'次这个数变成0')
i=0.8
c=0
while i<=1000000:
i=i*2
c+=1
print(c)
import random
print(random.randint(1,10))
random_num=random.randint(1,100)
star=1
stop=100
while True:
guess=int(input('请输入'+str(star)+'到'+str(stop)+'之间的数'))
if guess>random_num:
print('猜大了')
stop=guess
elif guess<random_num:
print('猜小了')
star = guess
else:
print('猜中了')
break