编写python程序、利用循环输出_第1章 Python基础-Python介绍&循环语句 练习题&作业...

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

高级语言分为编译型与解释型两种,分别从执行速度、开发效率、跨平台性三个方面说它们的区别。

编译型语言因为执行的是机器码文件,所以执行速度快且不依赖解释器,但每次修改源代码都需要重新编译,所以导致开发效率低,不同的操作系统,调用的底层机器指令不同,所以跨平台性差。

解释型语言需要解释器边把源文件解释成机器指令边交给cpu执行,所以执行速度要比编译型慢很多,但是每次修改时立刻见效,所以开发效率很高,解释器已经做好了对不同操作系统的交互处理,天生跨平台。

C/C++/C#/Delphi/Go属于编译型,PHP/Java/JavaScript/Python/Perl/Ruby属于解释型。

2.执行 Python 脚本的两种方式?

(1).交互方式:启动python解释器,执行命令

(2).脚本方式:Python xxx.py 或者 chmod +x xxx.py && ./xxx.py

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

单行注释:#要注释内容

多行注释:"""要注释内容""" 或者'''要注释内容'''

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

(1).变量由数字、字母和下划线组成

(2).变量不能以数字开头

(3).变量不能使用Python关键字

(4).变量区分大小写

模块名,包名 :小写字母, 单词之间用_分割。

类名:首字母大写。

全局变量: 大写字母, 单词之间用_分割。

普通变量: 小写字母, 单词之间用_分割。

函数: 小写字母, 单词之间用_分割。

实例变量: 以_开头,其他和普通变量一样 。

私有实例变量(外部访问会报错): 以__开头(2个下划线),其他和普通变量一样 。

专有变量: __开头,__结尾,一般为python的自有变量(不要以这种变量命名)。

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

id(变量名)

6.写代码

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

#!/usr/bin/env python

#-*- coding:utf-8 -*-

import getpass

_username = "seven"

_password = "123"

username = input("username:")

password = getpass.getpass("password:")

if username == _username and password == _password:

print("hello,seven")

else:

print("error,input again")

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

#!/usr/bin/env python

#-*- coding:utf-8 -*-

import getpass

_username = "seven"

_password = "123"

count = 0

while count < 3:

count += 1

username = input("username:")

password = getpass.getpass("password:")

if username == _username and password == _password:

print("hello,seven")

break

else:

print("error,input again")

#!/usr/bin/env python

#-*- coding:utf-8 -*-

import getpass

_username = 'seven'

_password = '123'

count = 0

def login():

username = input('username:')

password = getpass.getpass('password:')

return username,password

while count<3:

username,password = login()

if username == _username and password == _password:

print('hello,seven')

break

else:

count += 1

print ('error,input again')

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

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import getpass

_username = ['seven','alex']

_password = "123"

count = 0

while count < 3:

count += 1

username = input("用户名:")

# password = input("密码:")

password = getpass.getpass("密码:")

if username in _username and password == _password:

print("登陆成功!")

break

else:

print("登陆失败!")

7.写代码

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

# 2+4+6...+100

# -3-5...-99

count = 1

sum = 0

while count < 100:

count += 1

if count % 2 == 0 :

sum += count

else:

sum -= count

print(sum)

sum = 0

for count in range(2,101):

# print(count)

if count % 2 == 0 :

sum += count

else:

sum -= count

print(sum)

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

count = 0

while count <= 12:

if count == 6 or count == 10:

pass # 换成continue不行,因为会跳过本次循环,count不能+1,count永远==6,永远跳过本次循环。

else:

print(count)

count += 1

count = 0

while count < 12:

count += 1

if count == 6 or count == 10:

pass # 换成continue可以,因为虽然跳出了本次循环,但是下次循环的时候count可以+1。

else:

print(count)

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

count = 101

while count > 50:

count -= 1

print(count)

if count == 50:

count = 0

while count < 51:

print(count)

count += 1

break

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

count = 0

while count < 100:

count += 1

if count % 2 == 1:

print(count)

for count in range(1,101,2):

print(count)

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

count = 0

while count < 100:

count += 1

if count % 2 == 0:

print(count)

for count in range(2,101,2):

print(count)

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

n1 = 123456

n2 = n1

给数据123456起了另外一个别名n2,相当于n1和n2都指向该数据的内存地址

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

需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意显示

如:敬爱可爱的xxx,最喜欢在xxx地方干xxx

#方法一:

name = input("请输入姓名:")

address = input("请输入地点:")

hobby = input("请输入爱好:")

info = """

敬爱可爱的%s,最喜欢在%s地方干%s

"""% (name,address,hobby)

print(info)

#方法二:

name = input("请输入姓名:")

address = input("请输入地点:")

hobby = input("请输入爱好:")

info = """

敬爱可爱的{0},最喜欢在{1}地方干{2}

"""

print(info.format(name,address,hobby))

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

注:凡符合下面两个条件之一的年份是闰年。 (1) 能被4整除但不能被100整除。 (2) 能被400整除。

year = int(input("Please input the year:"))

if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:

print(year,"is a leap year")

else:

print(year,"is not a leap year")

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

money = 10000

years = 0

rate = 0.0325

while money <= 20000:

years += 1

money = money * (1+rate)

print(str(years)+"年以后,一万元的一年定期存款连本带息能翻番")

作业

编写登陆接口

基础需求:

让用户输入用户名密码

认证成功后显示欢迎信息

输错三次后退出程序

#!/usr/bin/env python3

# -*- encoding: utf8 -*-

# 输错用户名和输错密码的次数总共最多为3次

import getpass

exit_flag = False

count = 0

while count < 3 and not exit_flag:

user = input('\n请输入用户名:')

if user != "wss":

count += 1

print("\n用户名错误")

else:

while count < 3 and not exit_flag:

pwd = getpass.getpass('\n请输入密码:')

# pwd = input('\n请输入密码:')

if pwd == "123":

print('\n欢迎登陆')

print('..........')

exit_flag = True

else:

count += 1

print('\n密码错误')

continue

if count >= 3: # 尝试次数大于等于3时锁定用户

if user == "":

print("\n您输入的错误次数过多,且用户为空")

elif user != "wss":

print("\n您输入的错误次数过多,且用户 %s 不存在" % user)

else:

print("\n您输入的错误次数过多")

#!/usr/bin/python3

#-*- coding:utf-8 -*-

# 输错用户名和输错密码的次数分别最多为3次

# import getpass

_username = "wss"

_password = "123"

count = 0

exit_flag = False

while count < 3 and not exit_flag:

count += 1

username = input("\nPlease input your username:")

if username == _username:

exit_flag = True # 当密码正确时,跳过第一层循环,不再询问用户名

count = 0

while count < 3:

count += 1

password = input("\nPlease input your password:")

# password = getpass.getpass("\nPlease input your password:")

if password == _password:

print("\nhello,%s" % username)

break # 密码正确,跳过第二层循环,不再询问密码

else:

print("\nYour password is wrong")

else:

print("\nYour username is wrong")

if count >= 3: # 尝试次数大于等于3时强制退出

print("\nYou try more than 3 times,be forced to quit")

升级需求:

可以支持多个用户登录 (提示,通过列表存多个账户信息)

用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

#!/usr/bin/env python3

# -*- encoding: utf8 -*-

# 输错用户名和输错密码的次数总共最多为3次

import getpass

user_list = {

"wss": "123",

"alex": "456",

"jay": "789"

}

f = open("deny_user_list.txt", "a", encoding="utf-8") # 没有此文件时创建

f.close()

with open("deny_user_list.txt", "r", encoding="utf-8") as deny_user_list_file:

deny_user_list = deny_user_list_file.readlines()

exit_flag = False

count = 0

while count < 3 and not exit_flag:

user = input('\n请输入用户名:')

if user not in user_list:

count += 1

print("\n用户名错误")

elif user + "\n" in deny_user_list:

print("\n用户已被锁定,请联系管理员解锁后重新尝试")

break

else:

while count < 3 and not exit_flag:

pwd = getpass.getpass('\n请输入密码:')

# pwd = input('\n请输入密码:')

if pwd == user_list[user]:

print('\n欢迎登陆')

print('..........')

exit_flag = True

else:

count += 1

print('\n密码错误')

continue

if count >= 3: # 尝试次数大于等于3时锁定用户

if user == "":

print("\n您输入的错误次数过多,且用户为空")

elif user not in user_list:

print("\n您输入的错误次数过多,且用户 %s 不存在" % user)

else:

with open("deny_user_list.txt", "a", encoding="utf-8") as deny_user_list_file:

if user + "\n" not in deny_user_list:

deny_user_list_file.write(user + "\n")

print("\n您输入的错误次数过多,%s 已经被锁定" % user)

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# 输错用户名和输错密码的次数分别最多为3次

# import getpass

user_dict = dict([("wss", "123"), ("alex", "456"), ("jay", "789")]) # 存储用户信息

deny_user_list = [] # 初始化拒绝用户列表

f = open("deny_user_list.txt", "a", encoding="utf-8") # 没有此文件时创建

f.close()

with open("deny_user_list.txt", "r", encoding="utf-8") as deny_user_list_txt:

for deny_user in deny_user_list_txt.readlines(): # 将文件内容转换为列表

deny_user = deny_user.strip() # 去掉换行符

deny_user_list.append(deny_user)

count = 0

exit_flag = False

while count < 3 and not exit_flag:

count += 1

username = input("\nPlease input your username:")

if username in user_dict and username not in deny_user_list:

exit_flag = True # 当用户名正确时,跳过第一层循环,不再询问用户名

count = 0

while count < 3:

count += 1

password = input("\nPlease input your password:")

# password = getpass.getpass("\nPlease input your password:")

if password == user_dict[username]:

print("\nhello,%s" % username)

break # 当密码正确时,跳过第二层循环,不再循环密码

else:

print("\nYour password is wrong")

elif username in deny_user_list:

print("\n%s is locked,please contact the administrator" % username)

break

else:

print("\nYour username is wrong")

if count >= 3: # 当尝试次数大于等于3时强制退出并锁定用户

if username not in user_dict or username == "":

print("\nYou try more than 3 times,be forced to quit")

else:

with open("deny_user_list.txt", "a", encoding="utf-8") as deny_user_list_file:

if username not in deny_user_list:

deny_user_list_file.write(username + "\n")

print("\nYou try more than 3 times,%s has been locked" % username)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值