一、条件控制:
1、if语句
if-else
age = int(input("请输入年龄:"))
if age >= 18:
print("成年人")
else:
print("未成年")
print("结束")
age = int(input("请输入年龄:"))
if 18 <= age <= 65:
print("合法工作年龄")
else:
print("不合法")
print("结束")
if-elif-else
score = float(input("请输入成绩:"))
if 90 <= score <= 100:
print("优秀")
elif 80 <= score:
print("良好")
elif 70 <= score:
print("一般")
elif 60 <= score:
print("及格")
else:
print("不及格")
print("结束")
holiday = input("请输入节日:")
if holiday == "情人节":
print("玫瑰")
elif holiday == "圣诞节":
print("吃苹果")
elif holiday == "生日":
print("吃蛋糕")
else:
print("吃大餐")
2、if嵌套
ticlet = input("请出示你的车票:")
if ticket == "k51":
length = float(input("请输入刀具长度:"))
if length >= 15:
print("管制刀具,不能上车")
else:
print("可以上车")
else:
print("没有车票,不能上车")
money = float(input("请输入总价:"))
if money >= 500:
vip = input("请输入vip等级:")
if vip == "1":
money = money * 0.8
elif vip == "2":
money = money - money % 10
elif vip == "3":
money = int(money)
else:
sex = input("请输入性别:")
if sex == "男":
print("送皮带一条")
else:
print("送小包一个")
print("应付%.2f元"%money)
3、三元表达式:
a = 3
b = 4
c = a if a > b else b #如果a>b,c = a,否则c = b
print(c) #4
a = 3
b = 4
c = 5
c += a if a > b else b #如果a >b , c = c + a ,否则c = c + b
print(c) #9
a = 3
b = 4
c = 5
c *= a if a > b else b #如果a>b,c = c * a,否则c = c * b
print(c) #20
a = 3
b = 4
c = 5
c += a + b if a > b else b #如果a > b,c = c + (a + b),否则c = c + b
print(c) #9
a = 4
b = 3
c = 5
c *= a +b if a >b else b #如果a>b,c = c * (a +b),否则c = c * b
print(c) #35
二、循环结构:
1、while循环:
while 循环3要素:
1、初始值
2、控制条件
3、步长
计算1-100的和:
i = 1
sum1 = 0
while i <= 100:
sum1 += i
i += 1
print(sum1)
计算1-100内,所有偶数的和,奇数的和,能被3整除的数的和:
i = 1
jsh = 0
osh = 0
zc3 = 0
while i <= 100:
if i % 2 == 1:
jsh += i
else:
osh += i
if i % 3 == 0:
zc3 += i
i += 1
print("所有奇数和是:%d,所有偶数的和是:%d,所有能被3整除的数的和是:%d"%(jsh,osh,zc3))
求1 - 2 + 3 - 4 + 5…99的结果:
i = 1
x = 1
sum1 = 0
while i <= 99:
sum1 = sum1 + x * i
x *= -1
i += 1
print(sum1)
i = 1
sum1 = 0
while i <= 99:
if i % 2 == 1:
sum1 += i
else:
sum1 -= i
print(sum1)
输入n个数,求平均值:
count1 = int(input("请输入需要统计个数字个数:"))
sum1 = 0
i = 1
while i <= count1:
num = float(input("请输入第%d个数:"%i))
sum1 += num
average = sum1 / count1
print("平均数是%f"%average)
输入n个数,求最大值:
count1 = int(input("请输入需要比较的个数:"))
num1 = float(input("请输入第1个数:"))
max = num1
i = 1
while i <= count1:
num1 = float(input("请输入第%d个数:"%(i + 1)))
if num1 > max:
max = num1
i += 1
print("最大值是:%f"%max"))
按要求用*输出直角三角形图形:
i = 1
while i <= 5:
print("*" * i")
i += 1
while-else:
i = i
while i < 5:
print(i)
i+=1
else: #当控制条件不成立时,执行else
print(i,"程序走到这")
print("结束")
判断一个数是不是质数:
num1 = int(input("请输入一个正整数:"))
i = 2
while i < num1:
if num1 % i == 0:
print(i,"不是质数")
break #执行break后,跳出整个while-else循环
i += 1
else: #使用while-else可以减少判断次数
print(i,"是质数")
num1 = int(input("请输入一个正整数:"))
i = 2
while i < num1:
if num1 % i == 0:
print(i,"不是质数")
break
i += 1
if i == num1: #多判断了一次
print(i,"是质数")
循环次数 = 控制条件通过次数
i = 1
while 1 <= 10:
if i % 4 == 0:
break
print(i)
i += 1
i = 1
while i < 10:
if i % 3 == 0 or i % 7 == 0 or i % 10 == 0:
continue
print(i)
i += 1
2、for循环
遍历数字序列
for i in range(5,10,2):
print(i)
else:
print(i + 2,"不在范围内")
for i in range(15,10,-2):
print(i)
else:
print(i - 2,"不在范围内")
遍历字符串:
a = "12345"
for i in a:
print(a)
else:
print(i,"哈哈")
遍历元组:
a = (1,2,3,4,5)
for i in a:
print(a)
else:
print(i,"哈哈")
三、作业
1、用户登录模拟
while True:
user_name = input("请输入用户名:")
pass_word = input("请输入密码:")
if user_name == "ouyangheng" and pass_word == "666":
print("登录成功,欢迎使用")
break
else:
print("密码错误,请重新输入!")
2、猜拳游戏 3局2胜
import random
count = 1
win = 0
lose = 0
while count <= 3:
if win == 2 or lose == 2:
break
else:
user = int(float(input("请出拳:0(剪刀),1(石头),2(布):")))
if user != 0 and user != 1 and user != 2:
print("请重新输入正确数字")
continue
else:
data = ["剪刀","石头","布"]
com = random.randint(0,2)
print("你出{},电脑出{}".format(data[user],data[com]))
if user == com:
print("平局")
continue
elif user == 0 and com == 2 or user == 1 and com == 0 or user == 2 and com == 1:
win += 1
print("你赢了")
else:
lose += 1
print("你输了")
pass
count += 1
3、数字炸弹:
import random,time
bomb = random.randint(1,100)
star = 1
end = 100
while True:
num = int(input("请输入{}到{}之间的数字:".format(star,end)))
if num == bomb:
print("boom!你输了!")
break
elif num > bomb:
print("大了")
end = num - 1
else:
print("小了")
star = num + 1
print("请输入%d到%d之间的数字:"%(star,end))
time.sleep(1)
com = random.randint(star,end)
print("电脑输入的数字是%d"%com)
if com == bomb:
print("boom!电脑输了!")
break
elif com > bomb:
print("大了")
end = com - 1
else:
print("小了")
star = com + 1
print("数字炸弹是%d"%bomb)