zoukankan      html  css  js  c++  java
  • Python进阶之路——简单购物代码

    1.input 输入默认为string类型,如果需要使用输入的数字,要先转换为整型:


    2.需要显示元组序号的有两种方式:

     

    3.python里,比较符号可以简写:

    4.python里几种输出变量的方式,注意空格:

    5.python里可以高亮输出:

    结果为

    或者

     

    结果为;



    源代码:
    product_list = [
    ('iphone', 5000),
    ('mac Pro', 12000),
    ('bike', 800),
    ('book', 120),
    ('watch', 10600),
    ('coffee', 20)
    ]
    shopping_list = []
    salary = input("input you salary:")
    if salary.isdigit():
    salary = int(salary)
    while True:
    # for item in product_list:
    # print(product_list.index(item), item)
    for index, item in enumerate(product_list):
    print(index, item)
    user_choice = input("请输入想要买的商品序号:")
    if user_choice.isdigit():
    user_choice = int(user_choice)
    if 0 <= user_choice < len(product_list):
    p_item = product_list[user_choice]
    if p_item[1] <= salary:
    shopping_list.append(p_item)
    salary -= p_item[1]
    print("%s is added into your shopping_list,the current balance is 33[31;1m%s33[0m" % (p_item[0], salary))
    else:
    print("33[41;1m余额不足33[0m")
    else:
    print("33[41;1m商品不存在,请重新输入33[0m")

    elif user_choice == 'q':
    print("--------shopping_list--------")
    for p in shopping_list:
    print(p)
    print("your current salary:",salary)
    exit()
    else:
    print("33[41;1m invalid input33[0m")
    else:
    print("33[41;1m invalid input33[0m")









  • 相关阅读:
    js 跳转链接
    reg.test is not a function 报错
    html中button自动提交表单?
    mysql主从复制及双主复制
    nginx反向代理后端web服务器记录客户端ip地址
    mysql多实例-主从复制安装
    LVS+Keepalived高可用负载均衡集群架构实验-01
    debug调试
    常用网站总结
    项目部署
  • 原文地址:https://www.cnblogs.com/yzbt/p/8490875.html
Copyright © 2011-2022 走看看