• python基础 Day01 练习题


    1 字符串格式化

     1 #!/urs/bin/env python
     2 
     3 name = input("Name: ")
     4 age = int(input("Age: "))
     5 job = input("Job: ")
     6 salary = input("Salary: ")
     7 
     8 info1 = """
     9 
    10 ----------- info1 of %s ---------
    11 name: %s
    12 age: %s
    13 job: %s
    14 Salary: %s
    15 """ % (name, name, age, job, salary)
    16 
    17 print(info1)
    18 
    19 info2 = """
    20 
    21 ----------- info2 of {_name} ----
    22 Name: {_name}
    23 Age: {_age}
    24 Job: {_job}
    25 Salary: {_salary}
    26 """.format(_name=name,
    27            _age=age,
    28            _job=job,
    29            _salary=salary)
    30 
    31 print(info2)
    32 
    33 info3 = """
    34 
    35 ------ info3 of {0} -------
    36 Name: {0}
    37 Age: {1}
    38 Job: {2}
    39 Salary: {3}
    40 """.format(name, age, job, salary)
    41 
    42 print(info3)
    View Code

    2 密码非明文显示

    1 #!/urs/bin/env python
    2 # -*- coding:utf-8 -*-
    3 
    4 import getpass
    5 username = input("username:")
    6 password = getpass.getpass("password:")
    7 
    8 print(username)
    9 print(password)
    View Code

    注在Pycharm中不可以,在命令行模式运行:python xxx.py可以

     模拟登陆:
    1. 用户输入帐号密码进行登陆
    2. 用户信息保存在文件内
    3. 用户密码输入错误三次后锁定用户 
     1 #!/urs/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 
     5 lock_users = []
     6 f0 = open("lock_users.db", encoding="utf-8")
     7 for line in f0.readlines():
     8     line = line.strip()
     9     lock_users.append(line)
    10 f0.close()
    11 
    12 
    13 users_list = []
    14 f1 = open("users.db", "r",  encoding="utf-8")
    15 for line in f1.readlines():
    16     line = line.strip().split()
    17     users_list.append(line[0])
    18     users_list.append(line[1])
    19 f1.close()
    20 
    21 
    22 times = 3
    23 
    24 
    25 for i in range(times):
    26     username = input("Enter your name:")
    27     password = input("Enter your password:")
    28 
    29     if username in lock_users:
    30         print("	Sorry, you're locked now. Please contact administrators.")
    31         break
    32 
    33     if username in users_list and password == users_list[users_list.index(username) + 1]:
    34         print("	Welcome %s login !" % username)
    35         break
    36     else:
    37         print("	username or password is wrong.")
    38 
    39     if i == times-1:
    40         f2 = open("lock_users.db", "a+", encoding="utf-8")
    41         f2.write("
    ")
    42         f2.write(username)
    43         f2.close()
    44         print("	{}, you're locked!".format(username))
    View Code

    三级菜单:
    1. 运行程序输出第一级菜单
    2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
    3. 菜单数据保存在文件中
    4. 让用户选择是否要退出
    5. 有返回上一级菜单的功能
    评分标准:
    用多层嵌套while循环的方式完成作业2,85分
    只用一层循环完成作业2,100分

     1 #!/urs/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 data ={
     5     '北京':{
     6          '海淀区':{
     7              '颐和园', '圆明园遗址公园', '旧土城遗址公园', '大钟寺'
     8          },
     9         '东城区':{
    10             '劳动人民文化宫', '老舍纪念馆', '柳荫公园', '青年湖公园'
    11         },
    12         },
    13     '浙江':{
    14         '杭州':{
    15             '西湖', '西溪湿地', '宋城', '钱塘江', '南宋御街',
    16         },
    17         '绍兴':{
    18             '柯岩风景区、大禹陵、东湖、鲁迅故里',
    19         },
    20         },
    21     '山东':{
    22         '济南':{
    23             '趵突泉 大明湖 千佛山 芙蓉街(小吃街)',
    24         },
    25         '青岛':{
    26             '栈桥,小鱼山,海底世界,第一海水浴场,八大关风景区, 花石楼',
    27         },
    28         },
    29 }
    30 
    31 flag = True
    32 
    33 while flag:
    34     for i in data:
    35         print(i)
    36     choose1 = input(">")
    37 
    38     if choose1 == "q":
    39         flag = False
    40 
    41     while flag:
    42         for j in data[choose1]:
    43             print("	", j)
    44 
    45         choose2 = input(">>")
    46         if choose2 == 'b':
    47             break
    48         elif choose2 == 'q':
    49             flag = False
    50 
    51         while flag:
    52             for k in data[choose1][choose2]:
    53                 print("		",k)
    54 
    55             choose3 = input(">>>")
    56             if choose3 == 'b':
    57                 break
    58             elif choose3 == 'q':
    59                 flag = False
    View Code
     1 #!/urs/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 menu = {
     5     '北京':{
     6         '海淀':{
     7             '五道口':{
     8                 'soho':{},
     9                 '网易':{},
    10                 'google':{}
    11             },
    12             '中关村':{
    13                 '爱奇艺':{},
    14                 '汽车之家':{},
    15                 'youku':{},
    16             },
    17             '上地':{
    18                 '百度':{},
    19             },
    20         },
    21         '昌平':{
    22             '沙河':{
    23                 '老男孩':{},
    24                 '北航':{},
    25             },
    26             '天通苑':{},
    27             '回龙观':{},
    28         },
    29         '朝阳':{},
    30         '东城':{},
    31     },
    32     '上海':{
    33         '闵行':{
    34             "人民广场":{
    35                 '炸鸡店':{}
    36             }
    37         },
    38         '闸北':{
    39             '火车战':{
    40                 '携程':{}
    41             }
    42         },
    43         '浦东':{},
    44     },
    45     '山东':{},
    46 }
    47 
    48 
    49 exit_flag = False
    50 current_layer = menu
    51 
    52 layers = [menu]
    53 
    54 while not  exit_flag:
    55     for k in current_layer:
    56         print(k)
    57     choice = input(">>:").strip()
    58     if choice == "b":
    59         current_layer = layers[-1]
    60         #print("change to laster", current_layer)
    61         layers.pop()
    62     elif choice not  in current_layer:continue
    63     else:
    64         layers.append(current_layer)
    65         current_layer = current_layer[choice]
    View Code

     最多可连续3次猜年龄

     1 #!/urs/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 """
     5 最多可连续3次猜年龄
     6 """
     7 """
     8 _age = 56
     9 count = 3
    10 
    11 while count > 0:
    12     age = int(input("age = "))
    13     if age == _age:
    14         print("You're right.")
    15         break
    16     elif age > _age:
    17         print("think smaller ... ")
    18     else:
    19         print("think bigger ... ")
    20     count -= 1
    21 else:
    22     print("Sorry, there's not times for you.")
    23 """
    24 
    25 _age = 56
    26 
    27 for i in range(3):
    28     age = int(input("age = "))
    29     if age == _age:
    30         print("You're right.")
    31         break
    32     elif age > _age:
    33         print("think smaller ... ")
    34     else:
    35         print("think bigger ... ")
    36 else:
    37     print("Sorry, there's not times for you.")
    View Code

    可参看网址:

    http://www.cnblogs.com/alex3714/articles/5717620.html

  • 相关阅读:
    上标<sup>与下标<sub>
    Java7基础教程 Jay Bryant著 李鹏 韩智译
    Error:Could not find com.android.tools.build:gradle:2.14.1.
    nginx-rtmp-module安装笔记
    Page Scroll using Selenium WebDriver
    个人家庭宽带搭建多域名web网站服务器配置
    phpmyadmin mysql Access denied for user 'root'@'localhost'问题解决
    sizeWithFont 取消后
    根据size截取图片中间矩形区域的图片 这里的size是正方形
    给UIImageView添加点击事件
  • 原文地址:https://www.cnblogs.com/todayisafineday/p/6259020.html
Copyright © 2020-2023  润新知