Python练习题(一)

字典及列表

  1. 题目:在这里插入图片描述
    代码:
		n = int(input())
  		d = {}
  		for i in range(n):
  			a, b = (str(x) for x in input().split(' '))
  			d[b] = a
  		L = []
  		s = str(input())
  		while s != "dog":
  			L.append(d.get(s, "dog"))
  			s = str(input())
  		for c in L:
  			print(c)
  1. 题目:在这里插入图片描述
    在这里插入图片描述
    代码:
  		L = []
  		n = int(input())
  		for i in range(n):
  			str_m = input()
  		    num = str_m.split(' ')
  		    if num[0] == "1":
  		        stu_id = num[1]
  		        flag = 1
  		        for temp in L:
  		            if stu_id == temp['id']:
  		                print("Students already exist")
  		                flag = 0
  		                break
  		            else:
  		                continue
  		        if flag == 1:
  			        stu = {}
          			stu['id'] = num[1]
         			stu['name'] = num[2]
         			stu['score1'] = num[3]
         			stu['score2'] = num[4]
          			stu['score3'] = num[5]
          			L.append(stu)
          			print("Add success")
  			elif num[0] == "2":
 			    stu_id = num[1]
  			    flag = 1
  			    for temp in L:
      			    if stu_id == temp['id']:
          			    index_id = L.index(temp)
          			    flag = 0
              			break
          			else:
              			continue
      			if flag == 1:
          			print("Students do not exist")
      			else:
      			    del L[index_id]
          			print("Delete success")
  			elif num[0] == "3":
      			stu_id = num[1]
      			flag = 1
      			for temp in L:
         			 if stu_id == temp['id']:
              			temp['score1'] = num[2]
              			temp['score2'] = num[3]
              			temp['score3'] = num[4]
              			print("Update success")
              			flag = 0
              			break
          			else:
              			continue
      			if flag == 1:
          			print("Students do not exist")
  			elif num[0] == "4":
      			stu_id = num[1]
      			flag = 1
      			for temp in L:
          			if stu_id == temp['id']:
              			print("Student ID:" + temp['id'])
              			print("Name:" + temp['name'])
              			print("Average Score:%.1f" % ((int(temp['score1']) + int(temp['score2']) + int(temp['score3']))/3))
              			flag = 0
              			break
          			else:
              			continue
      			if flag == 1:
          			print("Students do not exist")
  1. 题目:
    在这里插入图片描述
    代码:
        c = input().lower()
        n = int(input())
        L=[]
        for i in range(n):
    		a = str(input())
    		if(a.lower().count(c) >= 3):
        		L.append(a)
		for i in range(-1, -len(L)-1, -1):#还可以用L.reverse()
    		print(L[i])
  1. 题目:
    在这里插入图片描述
    在这里插入图片描述
    代码:
   		n = int(input())
   		L = []
   		for i in range(n):
   			L.append(input())
   		c = input()
   		L = sorted(list(filter(lambda i: i.startswith(c), L)))
   		for i in L:
   			print(i)
  1. 题目:
    在这里插入图片描述
    代码:
   		n = int(input())
   		d = {}
   		for i in range(n):
   			a, b = map(str, input().split())
   			d.setdefault(b, []).append(a)
   		c = input()
   		L = sorted(d[c])
   		for i in L:
   			print(i)
  1. 题目:
    在这里插入图片描述
    代码:
		import math
		a, b = map(int, input().split())
		l = []
		for i in range(a, b + 1):
    		for j in range(2, int(math.sqrt(i)) + 2):
		        if i % j == 0:
            		break
    		else:
        	ii = str(i)[::-1]
        	if str(i) == ii:
            	l.append(i)
		for i in l:
    		print(i)
  1. 题目:
    在这里插入图片描述
    在这里插入图片描述
    代码:
		n = int(input())
		L = []
		for i in range(n):
    		b = {}
    		b['n'], b['s'], b['y'], b['m'], b['d'], b['v'], b['r'] = map(str, input().split())
		    L.append(b)
		L = sorted(L, key = lambda x : (int(x['v']), -int(x['y']), -int(x['m']), -int(x['d']), -int(x['s']), -int(x['r'])))
		for b in L:
    		print(b['n'], b['s'], b['y'], b['m'], b['d'], b['v'], b['r'])
  1. 题目:
    在这里插入图片描述
    在这里插入图片描述
    代码:
		import re
		n = int(input())
		d = {}
		for i in range(n):
    		a = input()
    		d[''.join(re.findall('[A-Z]+', a))] = a
		for i in sorted(d):
    		print(d[i])

文件

  1. 题目:
    在这里插入图片描述
    代码:
		# 绘制样式
		def draw(y, x):
			if (y > 58 and y < 69) or (x > 58 and x < 69):
				return 15  # 对应十六进制的 F
			else:
				return 0

		class Readbmp:
    		file = open("black.bmp", "rb")
    		# 读取 bmp 14字节的文件头
    		fileheader = file.read(14)
    		# 读取 bmp 40字节的位图信息头
    		infoheader = file.read(40)
    		# 读取 bmp 64字节的调色板
    		colorpalette = file.read(64)
			file.close()

		img = Readbmp()
		# 新建 test.bmp 文件
		test = open("test.bmp", "wb")
		# 写入源文件的文件头
		test.write(img.fileheader)
		# 写入源文件的位图信息头
		test.write(img.infoheader)
		# 写入源文件的调色板
		test.write(img.colorpalette)
		# 着色
		for i in range(0, 128):
    		for j in range(0, 64):
        		if draw(i, 2*j) == 0 and draw(i, 2*j + 1) == 0:  # 黑色
            		test.write(b'\x00')
        		elif draw(i, 2*j) == 15 and draw(i, 2*j + 1) == 0:
            		test.write(b'\xF0')
        		elif draw(i, 2*j) == 0 and draw(i, 2*j + 1) == 15:
            		test.write(b'\x0F')
        		elif draw(i, 2*j) == 15 and draw(i, 2*j + 1) == 15:  # 白色
            		test.write(b'\xFF')
		#        elif draw(i, 2*j) == 1 or draw(i, 2*j + 1) == 1:  # 暗红
		#            test.write(b'\x11')
		#        elif draw(i, 2*j) == 2 or draw(i, 2*j + 1) == 2:  # 绿色
		#            test.write(b'\x22')
		#        elif draw(i, 2*j) == 3 or draw(i, 2*j + 1) == 3:  # 灰绿
		#            test.write(b'\x33')
		#        elif draw(i, 2*j) == 4 or draw(i, 2*j + 1) == 4:  # 深蓝
		#            test.write(b'\x44')
		#        elif draw(i, 2*j) == 5 or draw(i, 2*j + 1) == 5:  # 紫色
		#            test.write(b'\x55')
		#        elif draw(i, 2*j) == 6 or draw(i, 2*j + 1) == 6:  # 蓝绿
		#            test.write(b'\x66')
		#        elif draw(i, 2*j) == 7 or draw(i, 2*j + 1) == 7:  # 灰色
		#            test.write(b'\x77')
		#        elif draw(i, 2*j) == 8 or draw(i, 2*j + 1) == 8:  # 灰白
		#            test.write(b'\x88')
		#        elif draw(i, 2*j) == 9 or draw(i, 2*j + 1) == 9:  # 亮红
		#            test.write(b'\x99')
		#        elif draw(i, 2*j) == 10 or draw(i, 2*j + 1) == 10:  # 亮绿色
		#            test.write(b'\xAA')
		#        elif draw(i, 2*j) == 11 or draw(i, 2*j + 1) == 11:  # 亮黄色
		#            test.write(b'\xBB')
		#        elif draw(i, 2*j) == 12 or draw(i, 2*j + 1) == 12:  # 亮蓝色
		#            test.write(b'\xCC')
		#        elif draw(i, 2*j) == 13 or draw(i, 2*j + 1) == 13:  # 粉紫色
		#            test.write(b'\xDD')
		#        elif draw(i, 2*j) == 14 or draw(i, 2*j + 1) == 14:  # 蓝白
		#            test.write(b'\xEE')

		test.close()

  1. 题目:
    在这里插入图片描述
    代码:
		def gcd(x, y):
    		while x % y != 0:
        		x, y = y, (x % y)
    		return y
    	
    	# 打印
    	def show(z, m):
    		z0 = z // gcd(z, m)
    		m0 = m // gcd(z, m)
        	print('{}/{}'.format(z0, m0))

		# 分数类
		class func:
    		def __init__(self, z, m):
        		self.z = z
        		self.m = m

    		def add(self, another):
        		z0 = self.z * another.m + self.m * another.z
        		m0 = self.m * another.m
        		show(z0, m0)

    		def sub(self, another):
        		z0 = self.z * another.m - self.m * another.z
        		m0 = self.m * another.m
        		show(z0, m0)

    		def mul(self, another):
        		z0 = self.z * another.z
        		m0 = self.m * another.m
        		show(z0, m0)

    		def div(self, another):
        		z0 = self.z * another.m
        		m0 = self.m * another.z
        		show(z0, m0)

    		def dao(self):
    			show(self.m, self.z)

		    def dec(self):
        		print("%.1f" % (self.z/self.m))


		a, b, c, d = map(int, input().split())
		f1 = func(a, b)
		f2 = func(c, d)
		show(f1.z, f1.m)
		show(f2.z, f2.m)
		f1.add(f2)
		f1.sub(f2)
		f1.mul(f2)
		f1.div(f2)
		f1.dao()
		f1.dec()

函数(全局变量)

  1. 题目:
    在这里插入图片描述
    在这里插入图片描述
    代码:
		all = 0  # 点数之和
		countA = 0  # (可以使用“点数加10”的)A牌数量
		useA = 0  # 手牌A牌“点数加10”使用次数

		# 客观价值————牌面,用于最后输出时排序
		def obj(o):
    		return {
        		'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7,
        		'8': 8, '9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13,
        		'Spade': 1, 'Heart': 2, 'Diamond': 3, 'Club': 4
    		}.get(o, 'error')

		# 主观价值————玩家定义的牌面,用于计算点数(牌面A另作讨论)
		def sub(s, x):
    		return {
        		'A': lambda x: x+1,
        		'2': lambda x: x+2,
        		'3': lambda x: x+3,
        		'4': lambda x: x+4,
        		'5': lambda x: x+5,
        		'6': lambda x: x+6,
        		'7': lambda x: x+7,
        		'8': lambda x: x+8,
        		'9': lambda x: x+9,
        		'10': lambda x: x+10,
        		'J': lambda x: x+10,
        		'Q': lambda x: x+10,
        		'K': lambda x: x+10
    		}[s](x)

		# 选择是否消耗手牌A牌的“点数加10”
		def use(al, cA, uA):
    		global all, countA, useA
    		while al <= 21 and cA > 0:
        		al += 10
        		cA -= 1
        		uA += 1
    		while al > 21 and uA > 0:
        		al -= 10
        		cA += 1
        		uA -= 1
    		all = al
    		countA = cA
    		useA = uA

		# 读入两张牌
		str1, str2 = input().split()
		str3, str4 = input().split()
		if str2 == 'A':
    		countA += 1
		if str4 == 'A':
    		countA += 1
		all = sub(str2, all)
		all = sub(str4, all)
		use(all, countA, useA)

		c = []  # 花色
		n = []  # 牌面
		c.extend([str1, str3])
		n.extend([str2, str4])

		# 游戏过程1:判断是不是Blackjack
		if all == 21:
    		flag = 1
		else:
    		flag = 0

		# 游戏过程2:要牌
		while all < 17:
    		str5, str6 = input().split()
    		c.append(str5)
    		n.append(str6)
    		print('Hit')  # 证明要牌
    		if str6 == 'A':
        		countA += 1
        		print('{} {} {}'.format(str5, 1, 11))
    		elif str6 == 'J' or str6 == 'K' or str6 == 'Q':
        		print('{} {}'.format(str5, 10))
    		else:
        		print(str5, obj(str6))
    		all = sub(str6, all)
    		use(all, countA, useA)

		# 游戏过程3:停止要牌并结算
		print('Stand')
		k = len(c)
		for i in range(0, k):
    		for j in range(i + 1, k):
        		if obj(n[i]) > obj(n[j]):
            		c[i], c[j] = c[j], c[i]
            		n[i], n[j] = n[j], n[i]
        		elif obj(n[i]) == obj(n[j]) and obj(c[i]) > obj(c[j]):
            		c[i], c[j] = c[j], c[i]
		for i in range(0, k - 1):
    		print('{}{}'.format(c[i], n[i]), end=' ')
		print('{}{}'.format(c[k-1], n[k-1]))
		if all > 21:
    		print('Bust')
		elif flag == 1:
    		print('Blackjack')
		else:
    		print(all)
  • 21
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值