第一模块:开发基础 第1章 练习及作业

练习题:

1. 简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型。

编译型:开发效率低,运行速度快
解释型:开发效率高,运行速度慢
编译型语言:C、C++
解释型语言:Python、Java

 

2. 执行Python脚本的两种方式是什么

1)python hollo.py
2) 在python脚本第一行加上 #!/usr/bin/python

 

3. Python单行注释和多行注释分别用什么?

单行注释:将#号置于注释行前
多汗注释:用三个单引号 ''' 注释内容 '''',或三个双引号 """ 注释内容 """

 

4. 布尔值分别有什么?

True 和 False

 

5. 声明变量注意事项有哪些?

只能包含字母、数字、下划线,不能以数字开头,变量名推荐使用下划线体,如shool_name = "oldboy"

 

6. 如何查看变量在内存中的地址?

name = "oldboy"
print(id(name))

 

7. 写代码:

1)实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则登录失败!

name = input("please input name:")
password = input("please input password:")
if name == "seven" and password == "123":
    print("登录成功")
else:
    print("登录失败")

 

2)实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则登录失败,失败时允许重复输入三次

count = 0
while count < 3:
    name = input("please input name:")
    password = input("please input password:")
    if name == "seven" and password == "123":
        print("登录成功")
        break
    else:
        print("登录失败")
    count += 1

 

3)实现用户输入用户名和密码,当用户名为seven或alex且密码为123时,显示登录成功,否则登录失败,失败时允许重复输入三次

count = 0
while count < 3:
    name = input("please input name:")
    password = input("please input password:")
    if name == "seven" and password == "123" or name == "alex" and password == "123":
        print("登录成功")
        break
    else:
        print("登录失败")
    count += 1

 

8. 写代码:

a. 使用while循环实现输出2-3+4-5+6...+100的和

mysum = 0
count = 2
while count <= 100:
    if count % 2 == 0:
        mysum += count
    else:
        mysum -= count
    count += 1
print(mysum)

 

b. 使用while循环实现输出 1,2,3,4,5,6,7,8,9,11,12

count = 0
while count <=12:
    if count == 10:
        pass
    else:
        print(count)
    count += 1

 

c. 使用while循环输出100-50,从大到小,如100,99,98...,到50时再从0循环输出到50,然后结束

flag = True
count = 100
while flag:
    print(count)
    if count == 50:
        count = 0
        while count <= 50:
            print(count)
            if count == 50:
                flag = False
                break
            count += 1
    else:
        count -= 1

 

 d. 使用while循环实现输出1-100内的所有基数

count = 1
while count <= 100:
    if count % 2 == 1:
        print(count)
    count += 1

 

e. 使用while循环实现输出1-100内的所有偶数

count = 1
while count <= 100:
    if count % 2 == 0:
        print(count)
    count += 1

 

 9. 现有如下两个变量,请简述n1和n2是什么关系?

n1 = 123456
n2 = n1
n1和n2都同时指向123456,当n1值发生改变时,n2依然指向的是123456

 

 10. 制作趣味模板程序(编程题)

需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意显示:
如:敬爱可爱的xxx,最喜欢在xxx地方干xxx
name = input("please input your name:")
address = input("please input your address:")
hobby = input("please inpout your hobby:")
print("敬爱可爱的%s,最喜欢在%s干%s" % (name, address, hobby))

 

11. 输入一年份,判断该年份是否闰年并输出结果。(编程题)

注:凡符合下面两个条件之一的年份是闰年。
(1)能被4整除但不能被100整除。
(2)能被400整除
year = int(input("please input year:"))
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    print("%s 是闰年" % (year))
else:
    print("不是")

 

 12. 假设一年期定期利率为3.25%,计算一下需要过多少年,一万元的一年定期存款连本带息能翻番?(编程题)

 

count = 0
money = 10000
while money < 20000:
    money = money + money * 3.25/100
    count += 1
print("存:%s年,金额是:%s" % (count, money))

 

 输出:

存:22年,金额是:20210.698678761957

 

 

作业:

编写登录接口

基础需求:
让用户输入用户名密码
认证成功后显示欢迎信息
输错三次后退出程序
'''
基础需求:
让用户输入用户名密码
认证成功后显示欢迎信息
输错三次后退出程序
'''
count = 0
while count < 3:
name = input("please input name:")
password = input("please input password:")
if name == "alex" and password == "123":
print("登录成功,欢迎 %s" % (name))
break
else:
print("用户名或密码错误")
count += 1

 

升级需求:
可以支持多个用户登录 (提示,通过列表存多个账户信息)
用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
'''
升级需求:
可以支持多个用户登录 (提示,通过列表存多个账户信息)
用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
'''
import os

user_list = ["alex", "tim"]
pass_list = ["123", "123"]
wrong_user_list = []
wrong_number_list = []

while True:
name = input("name:")
password = input("password:")
if os.path.exists("lock_account.txt"):
file_read = open("lock_account.txt", mode="r", encoding="UTF-8")
content = file_read.read()
file_read.close()
if name == content:
print("此账户已锁定。")
break
if name in user_list:
index_number = user_list.index(name)
if password == pass_list[index_number]:
print("登录成功")
break
else:
print("密码错误")
if name not in wrong_user_list:
wrong_user_list.append(name)
wrong_number_list.append(1)
else:
index_wrong_user = wrong_user_list.index(name)
wrong_number_list[index_wrong_user] += 1
if wrong_number_list[index_wrong_user] == 3:
print("你已经登录错误超过3次,账户已锁。")
f = open("lock_account.txt", mode="w", encoding="UTF-8")
f.write(name)
f.close()
break
else:
print("该用户不存在")

 



 

posted @ 2018-05-02 11:47  alexchenx  阅读(267)  评论(0编辑  收藏  举报