Shopping cart的程序编写

时间:2021-06-25 23:34:56
代码

# Author:Eric yin
shopping_cart =[]
# _quit = ""
# salary = int(input("Your salary is : "))
salary = (input("Your salary is : "))
Commodity_list = [("台灯",40),("Python 参考书",80),("鼠标",45),("草稿纸",10),("药品",40),("Iphone",6000)]
'''for i in Commodity_list:
print(Commodity_list.index(i),":", i)'''
while True:
if salary.isdigit():
salary = int(salary)
print("---------Commodity list----------")
for i, j in enumerate(Commodity_list,1):
print(i,j)
break
else:
print("Please enter Numeric types")
salary = (input("Your salary is : "))
print("-----------Buy goods-------------")
# while _quit != "~》{" :
while True :
Num_of_Comodity = input("Enter the number of comodity that you want:")
if Num_of_Comodity.isdigit():
Num_of_Comodity = int(Num_of_Comodity)
# if int(Num_of_Comodity) <7 :
if Num_of_Comodity <=len(Commodity_list) and Num_of_Comodity >= 0:
Commodity_list1 = Commodity_list[Num_of_Comodity-1]
if Commodity_list1[1] <= salary:
shopping_cart.append(Commodity_list1[0])
salary -= Commodity_list1[1]
if salary == 0:
print("You salary is 0!")
else:
pass
else:
print("Your salary is not enough, You can't buy any more!")
_quit1 = input("Do you want to continue shopping? Y&N")
# if _quit1 in "Yy" : 成员运算符in和not in应用于str list tuple三种数据类型
if _quit1 == "Y" or _quit1 =="y":
# 注意:和 _quit1 == "Y"or"y"的区别:==的优先级比or的优先级高,
# 所以是先比较_quit1与 "Y"是否相等,再与"y"求或,由于"y"永远为ture,所以,整句也为ture
continue
else:
# _quit = _quit1
break
else :
print("Enter wrong number,agin!")
else:
print("Please enter the number between \033[31;1m %s\033[0m and \033[31;1m %s\033[0m" %(1,len(Commodity_list)))

print("-----------Bought goods----------")
print("Have bought blew: ", end="")
for Bought_gooods in shopping_cart :
print(Bought_gooods,end=" ")
print("")
print("Balance salary:",salary)


改进的代码知识点:
1、for i, j in enumerate(Commodity_list,1):
print(i,j)
2、if _quit1 in "Yy" : 成员运算符in和not in应用于str list tuple三种数据类型;
3、函数.isdigit()、函数len()、内置函数enumerate();
4、高亮打印\033[31;1m %s\033[0m ;
5、print()默认换行,print(b,end="")不换行


# Author:Eric yin
shopping_cart =[]
# _quit = ""
# salary = int(input("Your salary is : "))
salary = (input("Your salary is : "))
Commodity_list = [("台灯",40),("Python 参考书",80),("鼠标",45),("草稿纸",10),("药品",40),("Iphone",6000)]
'''for i in Commodity_list:
print(Commodity_list.index(i),":", i)'''
while True:
if salary.isdigit():
salary = int(salary)
print("---------Commodity list----------")
for i, j in enumerate(Commodity_list,1):
print(i,j)
break
else:
print("Please enter Numeric types")
salary = (input("Your salary is : "))
print("-----------Buy goods-------------")
# while _quit != "~》{" :
while True :
Num_of_Comodity = input("Enter the number of comodity that you want:")
if Num_of_Comodity.isdigit():
Num_of_Comodity = int(Num_of_Comodity)
# if int(Num_of_Comodity) <7 :
if Num_of_Comodity <=len(Commodity_list) and Num_of_Comodity >= 0:
Commodity_list1 = Commodity_list[Num_of_Comodity-1]
if Commodity_list1[1] <= salary:
shopping_cart.append(Commodity_list1[0])
salary -= Commodity_list1[1]
if salary == 0:
print("You salary is 0!")
else:
pass
else:
print("Your salary is not enough, You can't buy any more!")
_quit1 = input("Do you want to continue shopping? Y&N")
# if _quit1 in "Yy" : 成员运算符in和not in应用于str list tuple三种数据类型
if _quit1 == "Y" or _quit1 =="y":
# 注意:和 _quit1 == "Y"or"y"的区别:==的优先级比or的优先级高,
# 所以是先比较_quit1与 "Y"是否相等,再与"y"求或,由于"y"永远为ture,所以,整句也为ture
continue
else:
# _quit = _quit1
break
else :
print("Enter wrong number,agin!")
else:
print("Please enter the number between \033[31;1m %s\033[0m and \033[31;1m %s\033[0m" %(1,len(Commodity_list)))

print("-----------Bought goods----------")
print("Have bought blew: ", end="")
for Bought_gooods in shopping_cart :
print(Bought_gooods,end=" ")
print("")
print("Balance salary:",salary)