# 3_循环语句()-其他

## 3_循环语句()

``if语句``
``````"""
# age = 17
# if age < 18:
#     print("抱歉，未满18岁禁止访问")
# else:
#     print("欢迎你来^o^")

# print("抱歉，未满18岁禁止访问") if age < 18 else  print("欢迎你来^o^")
#
# a = 3
# b = 5
# if a < b:
#     small = a
# else:
#     small = b
# print(small)
# ------------------------------
# small = a if a < b else b
# print(small)"""

"""temp = input("请输入一个分数：")
score = int(temp)
if 0 <= score < 60:
level = "D"
elif 60 <= score < 80:
level = "C"
elif 80 <= score < 90:
level = "B"
elif 90 <= score < 100:
level = "A"
elif  score == 100:
level = "S"
else:
level = "请输入0~100之间的分值^o^"
print(level)
-------------------------------------------
temp = input("请输入一个分数：")
score = int(temp)
level = ("D" if 0 <= score < 60 else
"C" if 60 <= score < 80 else
"B" if 80 <= score < 90 else
"A" if 90 <= score < 100 else
"S" if score == 100 else
"请输入0~100之间的分值^o^")
print(level)
"""

"""if语句嵌套
age = 20
isMan = True
if age < 18:
print("抱歉，未满18岁禁止访问")
else:
if isMan:
print("欢迎你来^o^")
else:
print("抱歉，本店不适合小公举哦")
-----------------------------------------
"""``````
``循坏结构（while）``
``````"""
i = 1
sum = 0
while i <= 100:
sum += i   # sum = sum + i
i += 1     # i = i + 1
print(sum)
---------------------------------
break语句每次只跳出一层循环
while True:
break
print("哎，好累哦~~~")

day = 1
hour = 1
while day <= 7:
while hour <= 8:
print("今天，我一定要坚持学习8个小时")
hour += 1
if hour > 1:
break
day += 1
-------------------------------------------
continue语句跳出当前循环
i = 0
while i < 10:
i += 1
if i % 2 == 0:
print("我是在continue里面的数字：" + str(i))
continue
print("外面的数字：" + str(i))
-----------------------------------

i = 1
while i < 5:
print("循环外，i的值是", i)
if i == 2:
break
i += 1
else:
print("循环外，i的值是", i)
--------------------------------------

i = 1
while i < 10:
j = 1
while j <= i:
print(j, "×", i, "=", j*i, end="  ")
j += 1
print()
i += 1
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
-----------------------------------------
"""``````
``for循环``
``````"""

for i in "i love you":
print(i)

i = 0
while i < len("i love you"):
print("i love you"[i])
i += 1
---------------------------------------
for循环求和
# 0 1 2 3 4 5 6 7 8 9 10
for i in range(11):
print(i)

# 5  6  7  8  9
for i in range(5, 10):
print(i)

#  5  7  9
for i in range(5, 10, 2):
print(i)

sum = 0
for i in range(101):
sum += i
print(sum)
---------------------------------

for n in range(2, 20):
for x in range(2, n):
if n % x == 0:
print(n, "不是素数")
break
else:
print(n, "是素数")
----------------------------------------
"""``````
————————
``if语句``
``````"""
# age = 17
# if age < 18:
#     print("抱歉，未满18岁禁止访问")
# else:
#     print("欢迎你来^o^")

# print("抱歉，未满18岁禁止访问") if age < 18 else  print("欢迎你来^o^")
#
# a = 3
# b = 5
# if a < b:
#     small = a
# else:
#     small = b
# print(small)
# ------------------------------
# small = a if a < b else b
# print(small)"""

"""temp = input("请输入一个分数：")
score = int(temp)
if 0 <= score < 60:
level = "D"
elif 60 <= score < 80:
level = "C"
elif 80 <= score < 90:
level = "B"
elif 90 <= score < 100:
level = "A"
elif  score == 100:
level = "S"
else:
level = "请输入0~100之间的分值^o^"
print(level)
-------------------------------------------
temp = input("请输入一个分数：")
score = int(temp)
level = ("D" if 0 <= score < 60 else
"C" if 60 <= score < 80 else
"B" if 80 <= score < 90 else
"A" if 90 <= score < 100 else
"S" if score == 100 else
"请输入0~100之间的分值^o^")
print(level)
"""

"""if语句嵌套
age = 20
isMan = True
if age < 18:
print("抱歉，未满18岁禁止访问")
else:
if isMan:
print("欢迎你来^o^")
else:
print("抱歉，本店不适合小公举哦")
-----------------------------------------
"""``````
``循坏结构（while）``
``````"""
i = 1
sum = 0
while i <= 100:
sum += i   # sum = sum + i
i += 1     # i = i + 1
print(sum)
---------------------------------
break语句每次只跳出一层循环
while True:
break
print("哎，好累哦~~~")

day = 1
hour = 1
while day <= 7:
while hour <= 8:
print("今天，我一定要坚持学习8个小时")
hour += 1
if hour > 1:
break
day += 1
-------------------------------------------
continue语句跳出当前循环
i = 0
while i < 10:
i += 1
if i % 2 == 0:
print("我是在continue里面的数字：" + str(i))
continue
print("外面的数字：" + str(i))
-----------------------------------

i = 1
while i < 5:
print("循环外，i的值是", i)
if i == 2:
break
i += 1
else:
print("循环外，i的值是", i)
--------------------------------------

i = 1
while i < 10:
j = 1
while j <= i:
print(j, "×", i, "=", j*i, end="  ")
j += 1
print()
i += 1
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
-----------------------------------------
"""``````
``for循环``
``````"""

for i in "i love you":
print(i)

i = 0
while i < len("i love you"):
print("i love you"[i])
i += 1
---------------------------------------
for循环求和
# 0 1 2 3 4 5 6 7 8 9 10
for i in range(11):
print(i)

# 5  6  7  8  9
for i in range(5, 10):
print(i)

#  5  7  9
for i in range(5, 10, 2):
print(i)

sum = 0
for i in range(101):
sum += i
print(sum)
---------------------------------

for n in range(2, 20):
for x in range(2, n):
if n % x == 0:
print(n, "不是素数")
break
else:
print(n, "是素数")
----------------------------------------
"""``````