本文主要是介绍Python初级第二次作业,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、
def reverse(num):a=numt=0b=0c=numwhile a//10>0:if a%10>=0:t+=1a=a//10print(t)for i in range(t,-1,-1):if c%10==0:b+=0else:b+=(c%10)*(10**i)c//=10print(b) if b==num:return Trueelse:return Falsedef isPalind(num):k=reverse(num)if k==True:print(f"{num}是回文")else:print(f"{num}不是是回文")num=int(input("请输入一个数字="))
isPalind(num)
二、
def reverse(num):a=numt=0b=0c=numwhile a//10>0:if a%10>=0:t+=1a=a//10for i in range(t,-1,-1):if c%10==0:b+=0else:b+=(c%10)*(10**i)c//=10if b==num:return Trueelse:return Falsedef su(num):for i in range(2,num):if num%i==0 :return False
t=0
n=1
while t<100:if n==1:print(n,end="\t")t+=1elif n==2:print(n,end="\t")t+=1else:r=reverse(n)s=su(n) if r==True and s!=False:print(n,end="\t")t+=1if t%10==0:print ()else:t+=0n+=1
三、
def su(num):for i in range(2,num):if num %i==0:return Falseelse:return True
print("1000以内的素数有:")
a=[1,2]
for i in range(3,1001):if su(i)!=False:a.append(i)s=0
for i in a:print(i,end="\t")s+=1if s%10==0:print()
print()
index=0
while index<1000:for i in range(0,len(a)):if (a[index]+2==a[i]):print(f"({a[index]},{a[i]})")index=i+1else:index+=1
四、
s=input("请按照ddd-dd-dddd的方式输入安全码=")
def safe(s):for i in range(0,len(s)):if i==3 or i==6:continueif s[i].isdigit()!=True:print("请输入数字!")return False
SSN="111-11-1111"
def true(s):if safe(s)==False:print("InValid SSN!")else:if s==SSN:print("Valid SSN!")else:print("InValid SSN!")
true(s)
五、
def findd(s1,s2):if s1.find(s2)!="":return True
s1=input("请输入一段长字符串=")
s2=input("请输入一段要搜索的子串=")
print(findd(s1,s2))
六、
def gcd(a):#将小数变成整数b=list(a)c=bs=0for i in b:if i%1!=0:i=int(i)c[s]=is+=1#找出最大值index=0for i in range (4,-1,-1):if a[index]<c[i]:index=i#最大公约数j=c[index]while c[0]%j!=0 or c[1]%j!=0 or c[2]%j!=0 or c[3]%j!=0 or c[4]%j!=0:j-=1return j a=eval(input("请输入五个数字="))
while len(a)!=5:print ("请输入5个数字")a=eval(input("请输入五个数字="))if len (a)==5:breakprint(f"最大公约数={gcd(a)}")
这篇关于Python初级第二次作业的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!