帮我补全这串代码,def main(): m,n =input("输入m,n:").split(",") m, n = int(m), int(n) if m <= 0 or n <= 0 print(“输入不是正整数!” else: if m > n: m,n = n, mcount =_[4]if count == 0: print("不存在素数!”) main(0)
时间: 2023-05-12 13:03:18 浏览: 170
def main():
m, n = input("输入m,n:").split(",")
m, n = int(m), int(n)
if m <= 0 or n <= 0:
print("输入不是正整数!")
else:
if m > n:
m, n = n, m
count = 0
for i in range(m, n+1):
if i == 2:
count += 1
elif i > 2:
for j in range(2, int(i**0.5)+1):
if i % j == 0:
break
else:
count += 1
if count == 0:
print("不存在素数!")
else:
print("素数的个数为:", count)
main()
相关问题
补全程序,完成以下功能:求[m,n]范围内的全部素数并输出。m、n是键盘输入的正整数,如果m、n有0及负整数,则输出“输入不是正整数!”;如果范围内有素数,则输出素数,若无素数,则输出“不存在素数!”。要求: 1)定义isPrime(m)函数,判断正整数m是否是素数,是素数返回True,否则返回False。2)定义prtPrime(m,n)函数,打印[m,n]范围内的素数,该函数返回值是找到的素数个数,同时控制一行最多是5个素数打印。import math def isPrime(m): if m == 1: return __________ for i in range(2, int(math.sqrt(m)) + 1): if_____________: return False else: ____________ def prtPrime(m, n): c = 0 print("[%d,%d]范围内的素数有:" % (m, n)) for i in range(m, n + 1): if _______: c += 1 if c % 5 == 0: print(i, end=' \n') else: print(i, end=' ') _________ def main(): m, n = input("输入m,n: ").(",") m, n = int(m), int(n) if m <= 0 or n <= 0: print("输入不是正整数!") ______ if m > n: m, n = n, m count = _____________ if count == 0: print("不存在素数!") main()
补全程序如下:
```
import math
def isPrime(m):
if m == 1:
return False
for i in range(2, int(math.sqrt(m)) + 1):
if m % i == 0:
return False
return True
def prtPrime(m, n):
c = 0
print("[%d,%d]范围内的素数有:" % (m, n))
for i in range(m, n + 1):
if isPrime(i):
c += 1
if c % 5 == 0:
print(i, end=' \n')
else:
print(i, end=' ')
return c
def main():
m, n = input("输入m,n: ").split(",")
m, n = int(m), int(n)
if m <= 0 or n <= 0:
print("输入不是正整数!")
return
if m > n:
m, n = n, m
count = prtPrime(m, n)
if count == 0:
print("不存在素数!")
main()
```
注意:在输入m,n时,需要用","分隔,例如输入"1,10"表示求[1,10]范围内的素数。
def regist(): username=input("输入用户名:") password= input("输入密码:") repassword=input("输入确认密码:") if password==repassword: with open("aaaa.txt",mode="a") as f: f.write("{} {} \n".format(username,password)) print("注册成功!") else: print("密码不一致") # regist() def login(): i=1 while True: username=input("输入ggg用户名:") password=input("输入a密码:") # while True: with open("a.txt")as s: while True: user=s.readline() input_user="{} {} \n".format(username,password) if user==input_user: print("登录成功") j='y' break else: # print("xxxxxxxx") break if j=='y': break i+=1 if i==4: print("账号锁定!") break login()改进后的代码
以下是改进后的代码:
```python
def regist():
username = input("输入用户名:")
password = input("输入密码:")
repassword = input("输入确认密码:")
if password == repassword:
with open("users.txt", mode="a") as f:
f.write("{} {} \n".format(username, password))
print("注册成功!")
else:
print("密码不一致")
def login():
i = 1
while i <= 3:
username = input("输入用户名:")
password = input("输入密码:")
with open("users.txt") as f:
for line in f.readlines():
user = line.strip().split()
if user[0] == username and user[1] == password:
print("登录成功!")
return
print("用户名或密码错误!")
i += 1
print("账号锁定!")
if __name__ == '__main__':
while True:
choice = input("请选择操作:\n1.注册\n2.登录\n3.退出\n")
if choice == '1':
regist()
elif choice == '2':
login()
elif choice == '3':
print("退出程序!")
break
else:
print("输入有误,请重新选择!")
```
改进的内容包括:
1. 将用户名和密码存储在一个文件中,以便于后续的登录验证;
2. 修改了登录函数的实现方式,使用了更加简洁和高效的方式来实现用户验证;
3. 增加了一个主函数,让用户可以选择注册、登录或退出程序;
4. 优化了代码的结构和风格,使其更加易读和易维护。
阅读全文
相关推荐

















