码迷,mamicode.com
首页 > 其他好文 > 详细

在线商城脚本学循环

时间:2019-12-07 21:11:08      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:隐式   lex   数字   phone   mda   直接   bre   注意   src   

————————————————————————————————————题     目—————————————————————————————————————————————

 

技术图片

 

————————————————————————————————————答     案—————————————————————————————————————————————

 

# commdity list
commodity = (["iphone" ,9998], ["bicycle", 1299],["coffic",38],["nothing", 1] )
basket = []

# key in your name and salay
name = input("key in your names : ")
salay = int(input("your salay : "))
#buy something
while True :
    i = 0
    print("\n\n")
    for commodity_name in commodity:
        print( str(i) + " : " + commodity[i][0] + str(commodity[i][1]) )
        i +=1

    chose = input("chose your commdity (‘q‘ to quit ): ")# chose commodity
    if chose == q :
        break
        #print(chose)
        #print(type(chose))
        #print(commodity[chose])
    # check money
    chose = int(chose)
    salay = salay -commodity[chose][1]   # delect money
    if salay <= 0:
        print("Your money isn‘t enough to buy " + commodity[chose][0] + " !")
        salay = salay + commodity[chose][1]
        break
    #input commodity to basket
    print("You chose the " + commodity[chose][0])
    if not basket == True :
        commdity_name = ["name", "price", 1]
        commdity_name[0] = commodity[chose][0]
        commdity_name[1] = commodity[chose][1]
        basket.append(commdity_name)
        print(basket)

    j = 0
    for x in basket :
        if x[0] == commodity[chose][0] :
            basket[j][2] +=1

            break
        else:
            commdity_name = ["name", "price", 1]
            commdity_name[0] = commodity[chose][0]
            commdity_name[1] = commodity[chose][1]
            basket.append(commdity_name)
        j += 1
        print(basket)

    #print(basket)
for x in basket :
    print("You bought " + x[0] + " ," + str(x[2]) + "s!")
print(name + ", you have " + str(salay) + " dollor now!")
print("Welcome next time!")

 

————————————————————————————————————参考答案—————————————————————————————————————————————

__author__ = "Alex Li"


product_list = [
    (Iphone,5800),
    (Mac Pro,9800),
    (Bike,800),
    (Watch,10600),
    (Coffee,31),
    (Alex Python,120),
]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            #print(product_list.index(item),item)
            print(index,item)
        user_choice = input("选择要买嘛?>>>:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice >=0:
                p_item = product_list[user_choice]
                if p_item[1] <= salary: #买的起
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )
                else:
                    print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m" % salary)
            else:
                print("product code [%s] is not exist!"% user_choice)
        elif user_choice == q:
            print("--------shopping list------")
            for p in shopping_list:
                print(p)
            print("Your current balance:",salary)
            exit()
        else:
            print("invalid option")

 

小结:

 

1、空集不能直接判断,要用 not 后进行判断

  Don’t check for empty values (like [] or ‘‘) by checking the length (if len(somelist) == 0). Use if not somelist and assume empty values implicity evaluate to False.
  意即,不要通过取字符串或者集合的长度来判断是否为空,而是要用not关键字来判断,因为当字符串或集合为空时,其值被隐式地赋为False.

  Ps. 集合包括list, tuple, dict

    if not basket == True :

2、 for i in names 的 i 是 集合元素 不是下标

names = ["张真","刘德华","哈林","谢霆锋","张柏芝"]
i = 0
for name in names :
    print(  "names : " + name)  #打印名字
    print(i)                   #第一种下标打印方式
    print(names.index( name )) #第二种下标打印方式
    x = names.index(name)      #下标提取方式
    print( "xiabiao " + str(x))
    i += 1

for index,item in enumerate(names):
  print(names.index(item),item)
  print(index,item)

 

3、打印输出时 注意转义成 str

4、两种列表输入格式 

product_list = [
    (Iphone,5800),
    (Mac Pro,9800),
    (Bike,800),
    (Watch,10600),
    (Coffee,31),
    (Alex Python,120),
]

commodity = (["iphone" ,9998], ["bicycle", 1299],["coffic",38],["nothing", 1] )

 5、判断输入的是否是数字

if i.isdigit():
    i = int( i )

6 、输出 变色 

 

 

 

在线商城脚本学循环

标签:隐式   lex   数字   phone   mda   直接   bre   注意   src   

原文地址:https://www.cnblogs.com/huanshuisi/p/12003257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!