num = 1000 whilenum >= 0: if num % 5 == 0and num % 7 == 0: print("可以同时被5和7整除的最大整数为: %d. " % num) break num -= 1
要求用户输入若干使用空格分割的数字,输出这些数字中只出现过一次的那些数字
1 2 3 4
nums = input('请以空格间隔输入若干数字:').split(' ') for num in nums: if nums.count(num) == 1: print(num)
生成一个包含15个介于1到50之间的随机整数的列表,为其循环左移5个元素
1 2 3 4 5 6 7
import random lst = random.sample(range(1, 51), 15) print(lst) for i inrange(5): lst.insert(len(lst), lst[0]) lst.remove(lst[0]) print(lst)
辗转相除法
1 2 3 4 5 6 7 8 9 10 11
defZhanZhuanChu(x, y): if x < y: x, y = y, x while y: r = x % y x = y y = r return x a = int(input("a的值为:")) b = int(input("b的值为:")) print(ZhanZhuanChu(a,b))
编写函数接收参数a和n,计算并返回形式如a + aa + aaa + aa…aa的表达式前n项的值,其中a为小于10的自然数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defSum_n(a, n): if a >= 10: return result = 0 for i inrange(1, n + 1): #range(1,n+1)表示从1~n tmp = 0 for j inrange(i): #range(i)从0开始 tmp = tmp + 10 ** j result = result + (a * tmp) return result a = int(input("a的值为:")) n = int(input("n的值为:")) print(Sum_n(a,n))
编写函数,接收字符串参数,返回一个元组,其中第一个元素为大写字母个数,第二个元素为小写字母个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
defdemo(s): #result = (0,0) 出错,因为元组不能被修改,只能先用列表再转元组 result = [0,0] tmp1,tmp2 = '','' for ch in s: if ch.islower(): result[1] += 1 tmp1 = tmp1 + ch elif ch.isupper(): result[0] += 1 tmp2 = tmp2 +ch print('小写字母有:' + tmp1 + ' 共有' + str(result[0]) + '个') print('大写字母有:' + tmp2 + ' 共有' + str(result[1]) + '个') returntuple(result) print(demo('This Is A Test'))
defdemo(n): defIsPrime(p): if p == 2: returnTrue # %2 求余数 if p % 2 == 0: returnFalse # 假如n是合数,必然存在非1的两个约数p1和p2,其中p1<=sqrt(n),p2>=sqrt(n) for i inrange(3,int(p**0.5)+1,2): if p % i == 0: returnFalse returnTrue ifisinstance(n,int) and n > 0and n % 2 == 0: for i inrange(2,n//2+1): if IsPrime(i) and IsPrime(n-i): print(i,'+',n-i,'=',n)
if __name__ == '__main__': demo(int(input("正偶数n的值为")))
defRate(orgin,userInput): ifnot (isinstance(orgin,str)) andisinstance((userInput,str)): print('Both Two Must Be Strings') return orgin,userInput = list(orgin),list(userInput) #right = sum(1 for i in range(1,len(userInput)) if orgin[i] == userInput[i]) #用生成器表达式,zip函数把orgin和userInput压缩在一起,生成一个对象。更优 right = sum(1for o,u inzip(orgin,userInput) if o == u ) returnround(right/len(orgin),2)
s1 = 'Read It Slow' s2 = 'really' print(Rate(s1,s2))
from random import randint defguess(maxValue=100,maxTimes=5): #随机生成一个数 value = randint(1,maxValue) for i inrange(maxTimes): prompt = 'Start to guess:'if i ==0else'Gusee again:' #使用异常处理结构,防止输入不是数字 try: x = int(input(prompt)) except: print('Must input an interger between 1 and',maxValue) else: #猜对了 if x == value: print("Congratuations!") break elif x > value: print("Too big") else: print("Too little")
else: #次数用完还没猜对,游戏结束,提示正确答案 print('Game over.Fail') print('The value is',value)
defmain(n): '''参数n表示数字的位数,例如n=3时返回495,n=4时返回6174''' #待测试数范围的起点和结束值 start = 10**(n-1) end = 10**n #依次测试每个数 for i inrange(start, end): #由这几个数字组成的最大数和最小数 big = ''.join(sorted(str(i),reverse=True)) little = ''.join(reversed(big)) big, little = map(int,(big, little)) if big-little == i: print(i) n = 4 main(n)
评论