Day4-总结

2019-06-09  本文已影响0人  SheeranED

一.if条件语句

1.if-elif-else结构

# 给一个年龄值,判断这个是儿童(0-12),少年(13-18),青年(19-28),中年(29-50)还是老年(50以上)
age = 28
if 0 < age <= 12:
    print('儿童')
elif 13 <= age <= 18:
    print('少年')
elif 19 <= age <= 28:
    print('青年')
elif 29 <= age <= 50:
    print('中年')
else:
    print('老年')

2.if嵌套

num = 23
if num % 2 == 0:
    print('偶数')
    if num % 4 == 0:
        print('能被4整除')
else:
    print('奇数')
    if num % 10 == 3:
        print('个位数是3')

二.for循环

for x in 'hello':
    print('====s')
for x in range(10):
    print(x)
for x in range(10, 100):
    print(x)
for x in range(0, 101, 2):
    print(x)
# 统计1000以内能被13整除的数的个数
count = 0
for x in range(13, 1001, 13):
    # print(x)
    count += 1
print(count)
# 统计1000以内能够被2整除但不能被5整除的个数
for x in range(1000):
    if x % 2 == 0 and x % 5 != 0:
        count += 1
print(count)
# 计算1+2+3...+100
sum1 = 0
for num in range(1, 101):
    sum1 += num
print(sum1)
# 计算1*2...*10
sum2 = 1
for num1 in range(1, 11):
    sum2 *= num1
print(sum2)

三.while循环

1.while循环

# 使用while循环控制程序,打印5行*****
x = 0
while x < 5:
    x += 1
# 使用while循环计算1+2....100
x = 1
sum1 = 0
while x <= 100:
    sum1 += x
    x += 1
print(sum1)

2.for循环与while循环的选择

# num = random.randint(0, 100)
# 猜数字游戏
# 随机产生一个数字,用户不断输入数字,直到输入的数字与产生的数字一样为止
num = random.randint(0, 100)
x = 0
print(num)
while True:
    new_x = int(input('请输入数字:'))
    if new_x == num:
        print('对了')
        break
    else:
        print('错了')

四.循环相关的关键字

1.continue关键字:

2.break关键字

3.else关键字

上一篇下一篇

猜你喜欢

热点阅读