python分支结构 python循环结构
顺序结构:一条一条语句执行 分支结构:某个判断条件后, 选择一条分支去执行
python分支结构
a = 2
if a == 1:
print('a==1')
else:
print('a!=1')
a = 0.1
if a == 1:
print('a==1')
elif a == 2:
print('a==2')
elif a == 0:
print('a==0')
else:
print('a不等于0,1,2')
x = -2
if x > 1:
y = 3 * x - 5
elif -1 <= x <= 1:
y = x + 2
elif x < -1:
y = 5 * x + 3
print(y)
x = 0
if x > 1:
y = 3 * x - 5
else:
if x < -1:
y = 5 * x + 3
elif -1 <= x <= 1:
y = x + 2
print(y)
result = 0
for i in range(101):
result = result + i
print(result)
result = 0
for i in range(101):
if i % 2 == 0:
result = result + i
print(result)
result = 0
for i in range(2, 101, 2):
result = result + i
print(result)
while True:
print(["a"])
a = 0
while a <= 5:
a = a + 1
print(a)
else:
print(a)
a = 0
while a == 1:
a = a + 1
else:
print('a!=1')
for i in range(1, 10):
if i == 5:
break
print(i)
for i in range(1, 10):
if i == 5:
continue
print(i)
"""
猜数字游戏
计算机出1-100之间的随机数由人来猜
计算机根据人猜的数字分别
给出提示大一点/小一点/猜对了
"""
import random
computer_number = random.randint(1, 100)
while True:
personal_number = int(input('请您输入数字'))
if personal_number > computer_number:
print('小一点')
elif personal_number < computer_number:
print('大一点')
elif personal_number == computer_number:
print('你猜对啦')
break
|