• python基础代码(猜年龄、从最内层跳出多层循环、简单的购物车程序)



    1、猜年龄 , 可以让用户最多猜三次!

    age = 55
    i=0
    while i<3:
        user_guess = int (input ("input your guess:"))
        if user_guess > age:
            print ("try smaller ")
    
        elif user_guess < age:
            print ("try bigger")
        else :
            print ("you got it")
            break
        i += 1
    

      

      

    2、猜年龄 ,每隔3次,问他一下,还想不想继续玩,y,n

    age = 55
    i=0
    while i<3:
        user_guess = int (input ("input your guess:"))
        if user_guess > age:
            print ("try smaller ")
    
        elif user_guess < age:
            print ("try bigger")
        else :
            print ("you got it")
            break
        i += 1
        print(i)
        if i % 3 == 0:
            y_or_n= input("will continue? 'y' or 'n':")
            if y_or_n == 'y':
                i=0
            elif y_or_n == 'n':
                break
    

    3、跳出多层循环,调用函数

    方法一:

    break_flag = False
    for i in range(5):
        print ("爷爷层",i)
        for j in range(5):
            print("爸爸层",j)
            if i ==3:
                break_flag = True
                break
            for k in range(5):
                print ("孙子层",k)
                if k ==2:
                    break_flag = True
                    break
            if break_flag :
                break
        if break_flag :
            print ("我出来了")
            break
    

      方法二:

    break_flag = False
    count = 0
    while break_flag == False:
        print("爷爷层")
        while break_flag == False:
            print("爸爸层")
            while break_flag == False:
                count +=1
                if count >5:
                    print ("孙子层")
                    break_flag = True
    print ("我出来了")
    

      

     方法三:

    跳出多层循环:(调用函数)
    def work():
        for i in range(5):
    	    for j in range(5):
    		    for k in range(5):
    			    if i == j == k == 2:
    			       return i,j,k
    			    else:
    				    print(i, '----', j, '----', k)
    
    print (work())
    

    4、购物车程序:


    功能要求:
    要求用户输入总资产,例如:50
    显示商品列表,让用户根据序号选择商品,加入购物车
    购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
    附加:可充值、某商品移除购物车
    """
    """
    1.创建空的购物车列表
    2.要求用户输入金额资产
    3.输入后列出商品列表
    4.用户选择商品,加入购物车
    5.结算,如果商品价格大于用户总资产提示余额不足,询问是否删除商品
    6.结算资金充裕,提示购买成功,显示余额并退出

     展示一:

    while True:
        print("num		goods		prices")
        for i in range(len(goods)):
            print(i + 1, ".		", goods[i], "		", prices[i])
        print("q .		quit")
        num1 = input("请输入需要的物品编号:")
        if num1 == 'q':
            if len(goods_buy) != 0:
                print("您购买的商品是:")
            for j in range(len(goods)):
                count1 = goods_buy.count(goods[j])
                if count1 > 0:
                    print(goods[j], prices[j] * count1)
            print("您还有 %d" % (salary))
            print("bye bye")
            break
        else:
            num=int(num1)
    
        if (num - 1) in range(len(goods)) and salary < prices[num - 1]:
            print("买不起,还差 %d " % (prices[num - 1] - salary))
        elif (num - 1) in range(len(goods)) and salary >= prices[num - 1]:
            salary = salary - prices[num - 1]
            goods_buy.append(goods[num - 1])
            print("放入购物车,扣钱,余额为 %d" % (salary))
        else:
            print("请输入商品列表中的标号")
    

    展示二:

    product_list = [['Iphone7',5800],
                    ['Coffee',30],
                    ['疙瘩汤',10],
                    ['Python Book',99],
                    ['Bike',199],
                    ['ViVo X9',2499],
                  ]
    
    shopping_cart = []
    salary = int(input("请输入你的工资:"))
    while True:
        num = 0
        for product in product_list:
            print(num,product)
            num += 1
        choice = input("请输入您所选的商品编号:").strip()
        if choice.isdigit():
            choice = int(choice)
            if choice >= 0 and choice < len(product_list):
                product = product_list[choice]
                if product[1] <= salary:
                    shopping_cart.append(product)
                    salary = salary - product[1]
                    print ("物品"+product[0]+"已经加入购物车","您的金额还剩:"+str(salary -product[1]) + ' 元')
                else:
                    print("余额不足,"+"还差:"+str(product[1]-salary)+"元")
            else:
                print ("商品不存在")
        elif choice == "q":
            print ("已经购买的商品有:")
            for i in shopping_cart:
                print (i)
    
            print ("您的余额为:",salary)
            print("end")
            break
        else:
            print ("无此选项!")
    

      

      

  • 相关阅读:
    计算机网络体系结构
    牛客多校第一场 Random Point in Triangle
    CSS line-height应用
    CSS line-height与行内框
    CSS 特殊性、继承与层叠
    Javascript进阶(6)---JS函数
    Javascript进阶(5)---闭包
    Javascript进阶(4)---编写类
    Javascript进阶(4)---几种特殊情况分析
    Javascript进阶(3)---原型链1
  • 原文地址:https://www.cnblogs.com/xuyuanyuan123/p/6627569.html
Copyright © 2020-2023  润新知