for循环
for 变量 in 范围:
代码块...
contune #跳出本次循环接着执行下一次循环
for 变量 in 范围:
代码块...
break #跳出本层循环,回到上一个for循环
else: #其实for循环和while循环都有else子句,不过是当循环完全执行了才会执行
代码块...
其他主程序代码块...
#作业1--跳出三层for循环
for i in range(5):
print("i---",i)
for j in range(5):
print("j---",j)
for k in range(5):
if k == 2:
print("k---",k)
break
else:
continue
break
else:
continue
'''
作业2-购物车程序
顾客可以根据自己的工资购买想要的东西,每次购买完成会计算剩余的金额
'''
print("----------Welcome to oldboy market----------")
print("------Today You can buy all of these:-------")
print("--Iphone<>Computer<>Coffee<>Book<>Clothes--")
shopping_car = []
client_salary = int(input("Please input your salary:"))
while True:
items = [['Iphone', 5000], ['Computer', 7999], ['Coffee', 30], ['Book', 80], ['Clother', 600]]
i = 1
enable_list = [] # 用户能购买的东西
disable_list = [] # 用户不能买的东西
cost_list = [] # 能购买的商品价格
name_list = [] # 能购买的商品名字
print("You can buy these:\n")
for item in items:
if item[1] <= client_salary:
enable_list.append(item)
cost_list.append(item[1])
name_list.append(item[0])
print(i, item[0], " ", item[1])
i += 1
else:
disable_list.append(item)
continue
print("Q quit shopping")
print("------------------------------------------------------")
print("You can not buy these,because you don't have enough money!\n", disable_list)
print("------------------------------------------------------")
choice = input("what's your choice number or quit:")
if choice == 'q' or choice == 'Q':
print("Today you have buy these :",shopping_car)
break
else:
choice = int(choice)-1
if choice <= len(enable_list):
if cost_list[choice] <= client_salary:
shopping_car.append(enable_list[choice]) #将选择的商品加到购物车
client_salary = client_salary - cost_list[choice]
print("The " ,enable_list[choice] , " is added to the shoppingcart \nyour balance is ",client_salary)
print("------------------------------------------------------")
print("Have Buy:",shopping_car)
print("------------------------------------------------------")
else:
print("You don't have enough money to buy this!")
continue
else:
print("Your choice is wrong,Please try again!")
continue
转载于:https://blog.51cto.com/altboy/1911207