zoukankan      html  css  js  c++  java
  • Python代码对比

    1、用户登陆

    • 输入用户名和密码
    • 认证成功后显示欢迎信息
    • 输错三次后锁定

    自己写的代码

     1 username="hello"
     2 passwd="123"
     3 name=input("your name:")
     4 pwd=input("your passwd:")
     5 i=0
     6 if username==name and passwd==pwd:
     7     print("你已成功登陆")
     8 else:
     9     print("用户名或密码错误,请重新输入")
    10     i+=1
    11     while i<3:
    12         name=input("your name:")
    13         pwd=input("your passwd:")
    14         if username==name and passwd==pwd:
    15             print("你已成功登陆")
    16             break
    17         elif i<=1:
    18             print("用户名或密码错误,请重新输入")
    19             if username==name and passwd==pwd:
    20                 print("你已成功登陆")
    21                 break
    22             i+=1
    23         else:
    24             print("你的账户被锁定")
    25             break
    26         

    简单的代码

     1 for i in range(3):
     2     username = input("Username:")
     3     password = input("Password:")
     4     if username == _user and password == _passwd :
     5         print("Welcome %s login...." % _user)
     6         passed_authentication = True #,真,成立
     7         break #跳出,中断
     8     else:
     9         print("Invalid username or password !")
    10 if not passed_authentication:#只有在True的情况下,条件成立
    11     print("要不要脸,臭流氓啊,小虎。")

    2、购物车程序

    salary = 5000
    1. iphone6s 5800
    2. mac book 9000
    3. coffee 32
    4. python book 80
    5. bicyle 1500
    >>>:1
    余额不足,-3000
    >>>:5
    已加入bicyle 到你的购物车, 当前余额:3500
    >>>:quit
    您已购买一下商品
    bicyle 1500
    coffee 30
    您的余额为:2970
    欢迎下次光临

    自己的代码

     1 salary=int(input("salary="))
     2 goods_list='''
     3 1.  iphone6s     5800
     4 2.  mac book     9000
     5 3.  coffee       32
     6 4.  python book  80
     7 5.  bicyle       1500
     8 '''
     9 print(goods_list)
    10 #用来判断五种物品是否购买的标志
    11 quit_buy=one=two=three=four=five=False
    12 
    13 while True:
    14     num = input()
    15     if num=='1':
    16         if salary>=5800:
    17             print("已加入iphone6s到你的购物车,当前余额:",salary-5800)
    18             salary = salary - 5800
    19             one=True
    20         else:
    21             print("余额不足!还剩:",salary)
    22     elif num=='2':
    23         if salary>=9000:
    24             print("已加入mac book到你的购物车,当前余额:",salary-9000)
    25             salary = salary - 9000
    26             two=True
    27         else:
    28             print("余额不足!还剩:",salary)
    29     elif num=='3':
    30         if salary>=32:
    31             print("已加入coffee到你的购物车,当前余额:", salary - 32)
    32             salary = salary - 32
    33             three=True
    34         else:
    35             print("余额不足!还剩:",salary)
    36     elif num=='4':
    37         if salary>=80:
    38             print("已加入python book到你的购物车,当前余额:", salary - 80)
    39             salary = salary - 80
    40             four=True
    41         else:
    42             print("余额不足!还剩:",salary)
    43     elif num=='5':
    44         if salary>=1500:
    45             print("已加入bicycle到你的购物车,当前余额:", salary - 1500)
    46             salary = salary - 1500
    47             five=True
    48         else:
    49             print("余额不足!还剩:",salary)
    50     elif num=='quit':
    51         quit_buy=True
    52         break
    53     else:
    54         print("请输入购物单上的序号")
    55 if quit_buy:
    56     if one==two==three==four==five==False:
    57         print("您未购买任何物品")
    58     else:
    59         print("您已购买以下商品")
    60     if one:
    61         print('iphone6s',5800)
    62     if two:
    63         print('mac book',9000)
    64     if three:
    65         print('coffee',32)
    66     if four:
    67         print('python book',80)
    68     if five:
    69         print('bicyle',1500)
    70 
    71     print("您的余额为:",salary)
    72     print("欢迎下次光临!")

    简单代码

     1 product_list=[
     2     ('Mac',9000),
     3     ('kindle',800),
     4     ('tesla',900000),
     5     ('python book',105),
     6     ('bike',2000),
     7 
     8 ]
     9 saving=input('please input your money:')
    10 shopping_car=[]
    11 if saving.isdigit():
    12     saving=int(saving)
    13     while True:
    14         #打印商品内容
    15         for i,v in enumerate(product_list,1):
    16             print(i,'>>>>',v)
    17 
    18          #引导用户选择商品
    19         choice=input('选择购买商品编号[退出:q]:')
    20 
    21         #验证输入是否合法
    22         if choice.isdigit():
    23             choice=int(choice)
    24             if choice>0 and choice<=len(product_list):
    25                 #将用户选择商品通过choice取出来
    26                 p_item=product_list[choice-1]
    27 
    28                 #如果钱够,用本金saving减去该商品价格,并将该商品加入购物车
    29                 if p_item[1]<saving:
    30                     saving-=p_item[1]
    31 
    32                     shopping_car.append(p_item)
    33 
    34                 else:
    35                     print('余额不足,还剩%s'%saving)
    36                 print(p_item)
    37             else:
    38                 print('编码不存在')
    39         elif choice=='q':
    40             print('------------您已经购买如下商品----------------')
    41             #循环遍历购物车里的商品,购物车存放的是已买商品
    42             for i in shopping_car:
    43                 print(i)
    44             print('您还剩%s元钱'%saving)
    45             break
    46         else:
    47             print('invalid input')

    自己写的代码不仅多,不简洁,而且还麻烦,初学Python还需加倍努力,争取写出更好的代码。

  • 相关阅读:
    WCF BasicHttpBinding 安全解析(2)BasicHttpBinding安全项
    WCF BasicHttpBinding 安全解析(4)windows验证(IIS宿主)
    网页优化系列三:使用压缩后置viewstate
    向IIS请求页面时,它做了什么?!(部分转载)
    使用Odbc链接mysql时parameter参数设置
    .net中close和dispose及关闭流操作
    asp.net 解码gb2312下urlencode后的字符串
    GridView实战一:自定义分页、排序、修改、插入、删除
    实现滑动分页(微博分页方式)
    javascript实例:逐条记录停顿的走马灯
  • 原文地址:https://www.cnblogs.com/cxli99/p/8412709.html
Copyright © 2011-2022 走看看