自学内容网 自学内容网

【头歌-Python】Python第五章作业(初级)

第1关:回文素数

def is_prime(num):
    if num < 2:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True

def is_palindrome(num):
    return str(num) == str(num)[::-1]

def generate_palindrome_primes(n):
    palindrome_primes = []
    num = 2
    while len(palindrome_primes) < n:
        if is_prime(num) and is_palindrome(num):
            palindrome_primes.append(num)
        num += 1
    return palindrome_primes

if __name__ == "__main__":
    n = int(input())
    result = generate_palindrome_primes(n)
    print(" ".join(map(str, result)))

第2关:反素数

def is_prime(num):
    if num < 2:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True

def is_palindrome(num):
    if str(num) == str(num)[::-1]:
        return False
    return True

def generate_palindrome_primes(n):
    palindrome_primes = []
    num = 2
    while len(palindrome_primes) < n:
        num1 = int(str(num)[::-1])
        if is_prime(num) and is_palindrome(num)and is_prime(num1):
            palindrome_primes.append(num)
        num += 1
    return palindrome_primes

if __name__ == "__main__":
    n = int(input())
    result = generate_palindrome_primes(n)
    print(" ".join(map(str, result)))

第3关:哥德巴赫猜想

def is_prime(n):
    """判断素数的函数,接收一个正整数为参数,参数是素数时返回True,否则返回False"""
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True


def goldbach_conjecture(num):
    """ 哥德巴赫猜想, 接收一个不小于4的正整数为参数。
    当参数为不小于4的偶数时,将其分解为两个素数的加和,按小数+数的格式输出。
    有多种组合时全部输出,但不输出重复的组合,例如输出8=3+5,不输出8=5+3。
    参数为奇数或小于4时,输出'Data error!'
    """
    if num % 2 != 0 or num < 4:
        print('Data error!')
    a = int(num/2)+1
    for i in range(1, a):
        if is_prime(i) and is_prime(num - i):
            print('{}={}+{}'.format(num, i, num - i))


if __name__ == '__main__':
    positive_even = int(input())  # 输入一个正数
    goldbach_conjecture(positive_even)

第4关:货币转换

a = input()
b = float(input())
str1=''
if a[- 1] != '¥' and a[- 1] != '$' or b < 0.0:
    print('Data error!')
elif a[- 1] == '$':
    for i in a[:-1]:
        if i != ' ':
            str1 = str1 + i
    str1 = float(str1)
    money = float(b * str1)
    print('{:.2f}¥'.format(money))
elif a[- 1] == '¥':
    for i in a[:-1]:
        if i != ' ':
            str1 = str1 + i
    str1 = float(str1)
    money = float(str1 / b)
    print('{:.2f}$'.format(money))

第5关:个人信息提取

n=input()
t=n.split(" ")
print('姓名:'+t[1])
print('班级:'+t[2])

j = 0
for i in t[4]:
    if i=='年':
        break
    j = j+1
print('出生:'+t[4][j-4:j+1])

第6关:身份证号基本信息

a = input()

str1 = a[6:10]
str2 = a[10:12]
str3 = a[12:14]

num = int(a[16])

print('出生:'+str1+'年'+str2+'月'+str3+'日')
if num%2==0:
    print('性别:女')
else:
    print('性别:男')

第7关:各位数字之和为5的数

n = int(input())

for i in range(0, n + 1):
    k = i
    a = k % 10 + int(k / 10) % 10 + int(k / 100) % 10
    if a == 5:
        print(i, end=" ")

第8关:字符串长度

print(len(input()))

第9关:字符串加密

import string

a = input()

lower = string.ascii_lowercase
upper = string.ascii_uppercase  

before = string.ascii_letters
after = lower[3:] + lower[:3] + upper[5:] + upper[:5]

table = "".maketrans(before, after)
print(a.translate(table))

第10关:输出单词

a = input()

t = a.split(" ")

for i in t:
    print(i)

第11关:字符大小写转换

n =input()

str1 = n.swapcase()

print(str1)

第12关:查找指定字符

a = input()
b =input()

index = b.find(a)

if index == -1:
    print('Not Found')
else:
    print('index = {}'.format(index))

第13关:随机密码生成器

import random
import string


n,s = map(int,input().split(','))

random.seed(s)

all_chars = string.digits + string.ascii_letters + '!"#$%&\\()*+,-.'

password = ''.join(random.choice(all_chars) for _ in range(n))

print(password)

第14关:模拟生成微软序列号

import random
import string


n = int(input())
s = int(input())

random.seed(s)

all_chars = r'BCEFGHJKMPQRTVWXY2346789'

for i in range(n):
    password = ''.join(random.choice(all_chars) for _ in range(5)) + '-' + ''.join(
        random.choice(all_chars) for _ in range(5)) + '-' + ''.join(
        random.choice(all_chars) for _ in range(5)) + '-' + ''.join(
        random.choice(all_chars) for _ in range(5)) + '-' + ''.join(random.choice(all_chars) for _ in range(5))
    print(password)

第15关:数列求和

a = int(input())
b = int(input())

if a>=1 and a<=9 and b>=0:
    sum = 0
    k = a
    if b ==0:
        print('0')
    else:
        for i in range(1,b+1):
            sum = sum + k
            k=k*10+a
        print(sum)

else:
    print('data error')

第16关:单词个数

a = input()
num=0
for i in a:
    if i==" ":
        num = num+1

print(num+1)


原文地址:https://blog.csdn.net/2301_78289455/article/details/138089521

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!