Python3程序设计实验一
文章摘自:Python3程序设计实验|PengWill
质因子
1.程序设计说明
编写函数,接受一个整数,返回该数所有的质因子。
2.程序设计思路
通过input函数读入一个字符串,用异常处理来判断其是否为合法的输入:若否,则抛出异常,否则继续执行。用分解质因数的方法循环处理出输入数字的所有质因数,将其加入集合。最后输出集合内容即可。
3.程序代码
while 1:
try:
num = int(input("Please input a number\n"))
except:
print("Invalid input")
else:
break
ans = set()
for i in range(2,num+1):
while num % i == 0:
num /= i
ans.add(i)
if num == 1:
break
print(ans)
强密码
1.程序设计说明
不使用正则表达式,判断是否满足强密码。
强密码条件为:
长度≥10;
只能够包含 大写字母,小写字母, 数字, 符号集(_!@#);
至少包含上述情况中的三种。
2.程序设计思路
通过input函数读入一个字符串,求其长度,若长度小于10,则为Weak Password,否则遍历字符串,判断其中是否包含小写字母,大写字母,数字或字符集中至少三种,若是则为Strong Password,否则为Weak Password。
3.程序代码
password = input()
length = len(password)
if length < 10:
print("Weak Password")
else:
judge = list([0]*4)
special = {'_','!','@','#'}
for i in password:
if i >='a' and i<='z':
judge[0] = 1
elif i>='A' and i<='Z':
judge[1] = 1
elif i>='0' and i<='9':
judge[2] = 1
elif i in special:
judge[3] = 1
if sum(judge) >=3:
print('Strong Password')
else:
print('Week Password')
邮箱正则表达式
1.程序编写说明
编写代码,获取 *.cn 页面中的信箱地址。
2.程序设计思路
首先使用urllib库爬页面的内容,然后根据使用正则表达式,筛选出符合邮箱特征的字符串,加以输出显示即可。
3.程序代码
import urllib.request
import re
file=urllib.request.urlopen('xxx.xxx.xxx')
data=file.read().decode('utf-8')
emailRegex = re.compile(r"([\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+)")
email = emailRegex.findall(data)
for item in email:
print(item[0])