美文网首页
python学习01-20180829-元组,字符串,三级菜单

python学习01-20180829-元组,字符串,三级菜单

作者: Habby的简书 | 来源:发表于2018-08-29 21:29 被阅读0次
  • 元组与购物车程序练习
    元组——就是只读而不能修改的列表

#Author:Habby

%购物车程序
'''
names=('alex',jack)
names.count()
names.index()

'''

'''
程序:购物车程序
需求:
    1.启动程序后,让用户输入工资,然后打印商品列表
    2.允许用户根据商品的编号购买商品
    3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    4.可以随时退出,退出时,打印已购买商品和余额
'''

product_list=[
    ('Iphone',5800),
    ('Mac Pro',9800),
    ('Bike',800),
    ('Coffe',31),
    ('Watch',10800)
    #('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[0m5000",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.")
  • 字符串常用操作
#Author:Habby

%增删改查等常用操作
name="My \tname is alex"

print(name.capitalize())
print(name.count("a"))
print(name.center(50,"-"))
print(name.endswith("ex"))
print(name.expandtabs(tabsize=30))
print(name[name.find("name"):])
print(name.isalnum())
print(name.isalpha())
print(name.isdecimal())
print(name.isdigit())
print(name.isidentifier())#判断是不是一个合法的标识符
print(name.islower())
  • 三级菜单实例
#Author:Habby

exit_flag=False
while not exit_flag:
    for i in data:
        print(i)

    choice= input("选择进入>>:")
    if choice in data:
        while not exit_flag:
            for i2 in data[choice]:
                print("\t",i2)
            choice2=input("选择进入>>:")
            if choice2 in data[choice]:
                while not exit_flag:
                    for i3 in data[choice][choice2]:
                        print("\t\t", i3)
                    choice3 = input("选择进入>>:")
                    if choice3 in data[choice][choice2]:
                        for i4 in data[choice][choice2][choice3]:
                            print("\t\t",i4)
                        choice4 = input("最后一层,按b返回>>:")
                        if choice4=="b":
                            pass
                        elif choice4=="q":
                            exit_flag=True
                    if choice3 == "b":
                        break
                    elif choice3 == "q":
                        exit_flag = True
            if choice2=="b":
                break
            elif choice2 == "q":
                exit_flag = True

相关文章

网友评论

      本文标题:python学习01-20180829-元组,字符串,三级菜单

      本文链接:https://www.haomeiwen.com/subject/jzqlwftx.html