1.3if判断语句+while和for循环语句+购物车作业

时间:2022-05-08 11:54:30

1、if 语句

if userame=_usename and password=_password:

print("welcome user {name} login...").fomat(name=username)

elif userame=_usename and password!=_password:

print("hello user {name} ,your password is wrong!").fomat(name=username)

else:

print("invalid username and password!")

#换行为python的功能表示子代码(即次级代码)

2、while循环语句

while True:

break                         #跳出循环

pass                          #什么都不做

else:

3、for循环语句

for i in range(0,10,3):    #i从0开始循环10次,隔3个开始一次

continue                        #直接下一循环

break                            #跳出循环

else:                              #for正常走完了之后执行(即没中途break)

4、购物车作业

购物车功能说明:输入工资,显示商品,输入商品编号购买商品

product_list=[
("book",100),
("coffee",50),
("table",200),
("shit",10),
("computer",10000)
]
shop_list=[]
shop_money=0 while True:
salary = input("input your salary>>>")
if salary.isdigit():
salary = int(salary)
break
else:
print("you must input number!") while True:
if salary == 0:
print("your money left 0 yuan,break")
break
if shop_list != []:
print("the goods you have picked:%s"%shop_list)
print("your salary balance %d yuan." % salary)
for goods in enumerate(product_list):
print(goods)
num=input("what do you want to bug?(you can input 0-4,or \"q\" to quit)")
if num.isdigit():
num = int(num)
if num >=0 and num <=4:
price = product_list[num]
if price[1] <= salary:
salary -=price[1]
shop_list.append(price[0])
shop_money +=price[1]
else:
print("you have no much money!") else:
print("input wrong digit!") elif num=="q":
break else:
print("input wrong digit!") print("the goods you have picked:%s"%shop_list)
print("it will cost you {cost} yuan,and your salary will balance {yuan} yuan".format(cost=shop_money,yuan=salary))