一、while循环语句
import random
num = random.randint(1, 100)
r_num = int(input())
i =1
while num != r_num:
if num < r_num:
print("偏大")
if num > r_num:
print("偏小")
r_num = int(input())
i +=1
print(f"第{i}次猜中")
二、不换行输出
print("hello",end='')
print("world",end='')
print("hei",end='')
print("hei")
print("hello",end='')
print("world",end='')
print("hei",end='')
换行输出,print()
输出一个空内容就是换行输出
三、while嵌套循环
i = 1
while i <=9:
j = 1
while j<=i-1:
print(f"{j}*{i}={j*i}\t",end='')
j += 1
print(f"{j}*{i}={j * i}\t")
i +=1
四、for循环语句
举例
str = 'I am lfp,I like handsome boy'
count = 0
for n in str:
if n == 'n':
count += 1
print(count)
五、range语句
六、for循环中的临时变量
七、for嵌套循环
for x in range(1,10):
for n in range(1,x+1):
print(f"{n}*{x}={n*x}\t",end='')
print()