users = {}
def register():
username = input("请输入用户名: ")
if username in users:
print("用户名已存在,请重新注册。")
return
password = input("请输入密码: ")
confirm_password = input("请再次输入密码: ")
if password != confirm_password:
print("两次输入的密码不一致,请重新注册。")
return
users[username] = password
print("注册成功!")
def login():
username = input("请输入用户名: ")
password = input("请输入密码: ")
if username not in users or users[username] != password:
print("用户名或密码错误,请重试。")
else:
print("登录成功!")
def main():
while True:
print("\n欢迎使用用户系统")
print("1. 注册")
print("2. 登录")
print("3. 退出")
choice = input("请选择操作: ")
if choice == "1":
register()
elif choice == "2":
login()
elif choice == "3":
print("感谢使用,再见!")
break
else:
print("无效的选择,请重新输入。")
if __name__ == "__main__":
main()
2.求50~150之间的质数是那些?
python复制代码
def zhishu(num):
for i in range(2, num // 2 + 1):
if num % i == 0:
return False
return True
for i in range(50, 151):
if zhishu(i):
print(f"{i}是质数")
3.打印输出标准水仙花数,输出这些水仙花数
python复制代码
def shuixianhua():
for num in range(100,1000):
i = num // 100
j = num // 10 % 10
k = num % 10
if num == i ** 3 + j ** 3 + k ** 3:
print ("输出的水仙花数为:%s"%(num))
shuixianhua()
4.验证:任意一个大于9的整数减去它的各位数字之和所得的差,一定能被9整除.
python复制代码
num = int (input("请输入大于9的数字:"))
if num<100:
i=num%10
j=num//10
sums=i+j
print(sums)
if (num-sums)%9==0:
print("该数能够被9整除,验证成功")
else:
print("该数能够被9整除,验证失败")
elif num>=100 and num<=1000:
i=(num %100)%10
j= (num %100)//10
k=num//100
sums=i+j+k
print(sums)
if (num -sums)/9==0:
print("该数能够被9整除,验证成功")
else:
print('这个数不能被9整除,验证失败')
a=[10, 8, 5, 3, 27, 99]
x=0
for i in a:
if i%2==0:
x+=i//2
else:
x+=i//2+1
print("最少%s"%(x)+"次可以拿完硬币")
7.如果两个素数之差为2,这样的两个素数就叫作"孪生数",找出100以内的所有"孪生数".
python复制代码
twin_primes = []
for num in range(2, 100):
# 判断一个数是不是素数
primes = True
for i in range(2, int(num ** 1 / 2) + 1):
if num % i == 0:
primes = False
break
# 如果说一个数是素数,与它相差为2的另一个数也是素数那么这一对数成为孪生数
if primes == True:
next_num = num + 2
next_primes = True
for i in range(2, int(next_num ** 0.5) + 1):
if next_num % i == 0:
next_primes = False
break
if next_primes == True:
twin_primes.append((num, next_num))
# 将100以内的所有孪生数输出来
for twin_prime in twin_primes:
print(twin_prime)
8.给定一个列表,求最大值(不能使用系统api),求最小值,求平均值、求和
python复制代码
a = [12, 14, 24, 64, 58, 32]
# 计算最大值
max_num = a[0]
for num in a:
if num > max_num:
max_num = num
# 计算最小值
min_num = a[0]
for num in a:
if num < min_num:
min_num = num
# 计算总和
sum = 0
for num in a:
sum += num
# 计算平均值
average = sum / len(a)
print("最大值:", max_num)
print("最小值:", min_num)
print("总和:", sum)
print("平均值:", average)
arr_list = [1, 1, 2, 2, 3, 3, 4, 5, 9]
print("原始列表为:", arr_list)
unique_list = []
for num in arr_list:
if num not in unique_list:
unique_list.append(num)
print("去除重复后的列表为:", unique_list)
10.如何将0-10随机存入列表中
python复制代码
import random
ls = []
for i in range(10):
ls.append(random.randint(0, 10))
print(ls)