挑战每日打卡python基础题
come with me !今日练习:判断语句,for循环,break,continue
1、嵌套循环输出10-50中,个位带1-5的所有数字
# 分析:11-15,21-25,31-35,41-45
for i in range(1,5):
for j in range(1,6):
print(i*10+j,end=',')
print()
for i in range(10,51):
n = i%10 # 取余数,就是个人数
if n in [1,2,3,4,5]:
print(i)
#结果:
11,12,13,14,15,21,22,23,24,25,31,32,33,34,35,41,42,43,44,45,
11,12,13,14,15,21,22,23,24,25,31,32,33,34,35,41,42,43,44,45,
2、计算随机数之和,超过100停止,随机数1-20的范围产生,要求记录一下随机数, 最后的和,以及随机数的个数。
import random
count1 = 0
num2 = 0
list1 = []
# for i in range(1000):
while 1:
if count1 > 100:
break
else:
num1 = random.randint(1, 20)
count1 += num1
num2 += 1
list1.append(num1)
print(num2)
print(count1)
print(list1)
#结果:
11
116
[7, 15, 4, 12, 15, 12, 7, 9, 18, 1, 16]
3、10000个随机数相乘,耗时多久
import time,random
t1 = time.time()
count1 = 0
result = 0
for i in range(1,10000):
if count1 < 10000:
a = random.random()
result *= a
else:
break
t2 = time.time()
print(t2-t1)
#结果:0.003988504409790039
break与continue区别
4、使用continue,得出10以内的奇数
continue 语句是一个删除的效果,他的存在是为了删除满足循环条件下的某些不需要的成分
n = 0
while n <10:
n += 1
if n % 2 == 0: # 偶数都跳过
continue
print(n,end=',')
# 结果:1,3,5,7,9,
学python,跟学其他语言一样,多练才能更熟练~
come on !
网友评论