目录
一.if 判断
二.elif 多分支
三.while 循环
四.for 循环
五.购物车程序
一.if 判断
代码段
user = 'joy'
password = '123456'
# 输入用户名和密码
user_account = input('please input user:')
user_password = input('please input password:')
# 判断用户名和密码是否正确
if user == user_account and password == user_password :
print('登录成功')
else:
print('用户名或密码错误')
输出结果
please input user:abb
please input password:2387
用户名或密码错误
二.elif 多分支
代码段A (使用 if + else 实现)
result = int(input("你的成绩是:"))
if result > 80 and result <= 100 :
print("优秀成绩")
else :
if result <= 80 and result >= 60 :
print("中等成绩")
else:
if result < 60 :
print("成绩较差")
else :
print("成绩异常")
代码段B (使用 if + elif + else 实现)
result = int(input("你的成绩是:")) # input默认为str类型,需要转换成int类型
if result > 80 and result <= 100 :
print("优秀成绩")
elif result <= 80 and result >= 60 :
print("中等成绩")
elif result < 60 :
print("成绩较差")
else :
print("成绩异常")
输出结果
你的成绩是:65
中等成绩
三.while 循环
代码段
# while 递归
a = 1
c = 1
while a <= 100 :
b = 0
while b <= 9 :
b += 1
print(a,end=" ") # 打印一个数字,end留一个空格
a += 1
print("".strip("\n")) # strip去除字符串首位转义字符,换行、空格
else :
print("end")
输出结果
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
end
四.for 循环
主要用来遍历、循环、序列或者集合、字典
代码段
goods = [['ipad','iphone','iwatch','imacbook','headphone'],(4800,6500,3200,150000,2400)]
for i in goods:
for x in i:
print(x,end=" ")
else:
print('fruit is gone')
输出结果
ipad iphone iwatch imacbook headphone 4800 6500 3200 150000 2400 fruit is gone
1.break 跳出
代码段
goods = ['ipad','iphone','iwatch','imacbook','headphone']
for i in goods:
if i == 'iwatch':
break # 跳出,终止程序
print(i)
输出结果
ipad
iphone
2.continue 跳过、继续
代码段
goods = ['ipad','iphone','iwatch','imacbook','headphone']
for i in goods:
if i == 'iwatch':
continue # 跳过,继续
print(i)
输出结果
ipad
iphone
imacbook
headphone
3.嵌套循环
代码段
goods = [['ipad','iphone','iwatch','imacbook','headphone'],(4800,6500,3200,150000,2400)]
for i in goods:
# print(i)
for x in i:
# print(x)
if x == 'iwatch':
break # break只会影响同一级的for循环,其他for循环正常运行
print(x,end=" ")
else:
print('fruit is gone')
输出结果
ipad
iphone
imacbook
headphone
ipad iphone 4800 6500 3200 150000 2400 fruit is gone
4.阵列
代码段
for x in range(2,101,2):
print(x,end='|')
print("\n")
for y in range(100,1,-2): # 倒序
print(y,end='|')
输出结果
2|4|6|8|10|12|14|16|18|20|22|24|26|28|30|32|34|36|38|40|42|44|46|48|50|52|54|56|58|60|62|64|66|68|70|72|74|76|78|80|82|84|86|88|90|92|94|96|98|100|
100|98|96|94|92|90|88|86|84|82|80|78|76|74|72|70|68|66|64|62|60|58|56|54|52|50|48|46|44|42|40|38|36|34|32|30|28|26|24|22|20|18|16|14|12|10|8|6|4|2|
5.列表阵列
代码段
number = [1,2,3,4,5,6,7,8,9,10]
for a in number[0:len(number):2]: # 方法一:使用切片
print(a,end=' ')
print("\n")
for b in range(1,len(number),2): # 方法二:使用range方法
print(b,end=' ')
输出结果
1 3 5 7 9
1 3 5 7 9
五.购物车程序
思路
商品列表
账户金额
选择商品
加入购物车
退出结算
product_list = [
('macbook',12000),
('iphone 11',5500),
('freebuds3',1200),
('watch GT2',1680),
('matepad pro',3200),
('sound x',1999),
('mate x',16999)
]
shoping_car = []
# print(product_list)
while True:
# 用户输入存款,判断是否为字符串
saving = input('目前存款【退出:q】:')
if saving.isdigit():
saving = int(saving)
while True:
# 打印购物车,给商品编号从1开始
for i,v in enumerate(product_list,1):
print(i,'>>>',v)
# 用户输入商品编号,半段是否为字符串,输入q退出
choice = input('请输入您需要购买商品的编号【退出:q】:')
if choice.isdigit():
choice = int(choice)
# 判断输入商品编号,是否符合规则
if choice >=0 and choice <= len(product_list):
p_item = product_list[choice - 1]
#判断商品是否小于存款
if p_item[1] < saving:
saving -= p_item[1]
shoping_car.append(p_item)
else:
print('余额不足,还剩%s'%saving)
print(p_item)
else:
print('您选择的商品编码不存在')
elif choice == 'q':
print('------您购买的商品如下------')
for i in shoping_car:
print(i)
print('您还剩下金额%s'%saving,'元')
break
else:
print('您输入的商品编号有误')
# # print('您选择的商品如下:',p_item[0])
# print('剩余存款:',int(saving)-p_item[1])
break
elif saving == 'q':
print("您已退出存款")
break
else:
print('您输入的存款有误,请重新输入')