# while循环
# 通过while循环,计算1到100的总和
num = 1
sum = 0
while num <= 100:
sum = num + sum
num += 1
print(f"1到100的和为{sum}")
#嵌套语句--实现猜1-10数字游戏
import random
flag=True
num=random.randint(1,10)
while flag:
guess_num=int(input("请输入你猜想的数字:"))
if guess_num==num:
print("你猜中了!")
flag=False
else:
if guess_num<num:
print("你猜小了")
else:
print("你猜大了")
# 1-9的乘法口诀表
i=1
while i<=9:
j=1
while j<=i:
print(f"{j}*{i}={j*i}",end=" ")
j+=1
print("\t")
i+=1
print("以上就是1-9的乘法口诀表请查收!")
for循环
# for循环
# for 变量 in 被处理的数据
name="Hello"
for x in name:
print(x,end=",")
count_y=0
str= "t\pystudyenv\Scripts\python.exe D:/yt/pystudy/venv/day03.py"
for x in str:
print(x,end=",")
if x=="y":
count_y+=1
print("\t")
print(f"这个字符串中共有字母y{count_y}个")
# range 语法
# 从1-5不包括5的所有数字
r=range(1,5)
print(r)
for x in r:
print(x)