# 随机4到6位的验证码,验证码包含大小写字母和数字。用户输入验证码,忽略大小写。
# 然后进行验证,如果字母(不分大小写)和数字相同,则成功,反之重来
1 import random, string 2 class Random1(): 3 def __init__(self): 4 self.number = 0 5 self.aZ = 0 6 def show(self): 7 while True: 8 a1 = random.randint(4, 6) 9 a = "%s%s" % (self.number, self.aZ) 10 rr = "" 11 for i in range(a1): 12 rr += random.choice(a) 13 print(rr) 14 return str(rr).lower() 15 r1 = Random1() 16 r1.number = string.digits 17 r1.aZ = string.ascii_letters 18 def shuru(): 19 while True: 20 a = r1.show() 21 num = input("输入验证码") 22 num = str(num).lower() 23 if a == num: 24 print("成功") 25 break 26 else: 27 print("验证码错误,请重新输入") 28 pass 29 shuru()
# 用户在注册账号的时候我们需要输入用户信息,
# 创建一个用户类,变量有:用户名、密码、性别。
# 要求:用户名必须以字母开头,密码必须是6位以上,性别只能是男和女。
# 写一个show方法打印用户名、密码、性别的信息。
# 在主函数中创建一个用户对象,给三个变量赋值,
# 如果赋值不满足要求则重新输入。否则调用show方法输出信息即可
1 import string 2 3 4 class User(): 5 def __init__(self): 6 self.__name = "" 7 self.__password = "" 8 self.__sex = "" 9 10 def get_user_show(self): 11 print("信息如下") 12 return "姓名%s 密码%s 性别%s" % (self.__name, self.__password, self.__sex) 13 14 def set_user_show(self): 15 quan_dui = False 16 while True: 17 if quan_dui == True: 18 break 19 name = input("输入用户名") 20 if name[0] in string.ascii_letters: 21 self.__name = name 22 else: 23 print("不合法") 24 continue 25 while True: 26 if quan_dui == True: 27 break 28 password = input("输入密码") 29 if len(password) >= 6: 30 self.__password = password 31 else: 32 print("不合法") 33 continue 34 while True: 35 sex = input("输入性别") 36 if sex in "男" or sex in "女": 37 self.__sex = sex 38 quan_dui = True 39 40 break 41 else: 42 print("不合法") 43 44 45 u1 = User() 46 u1.set_user_show() 47 print(u1.get_user_show())
# 定义一个英雄类,包含玩家数量,生命值,攻击力和玩家姓名,
# 同时包含一个方法攻击方法,调用攻击方法时显示如图信息,
# 构造函数中需要初始化数据,当玩家加入游戏后需要显示玩家姓名和数量
1 class player(): 2 def __init__(self, name, art, hp, number): 3 self.name = name 4 self.art = art 5 self.hp = hp 6 self.number = number 7 8 def join(self): 9 print("新玩家%s加入游戏,当前玩家数为%d" % (self.name, self.number)) 10 11 def artte(self): 12 i = 0 13 while player1.hp > 0: 14 player1.hp = player1.hp - player2.art 15 print("玩家%s受到玩家%s的%d点伤害,hp剩余%d" 16 % (player1.name, player2.name, player2.art, player1.hp)) 17 i += 1 18 if player1.hp == 0: 19 break 20 if i == 2 : 21 i -= 2 22 while player2.hp > 0: 23 player2.hp = player2.hp - player1.art 24 print("玩家%s受到玩家%s的%d点伤害,hp剩余%d" 25 % (player2.name, player1.name, player1.art, player2.hp)) 26 i += 1 27 if player2.hp == 0: 28 break 29 if i == 2: 30 i -= 2 31 input("请安任意键继续") 32 break 33 num = 1 34 while True: 35 player1 = player("Tom", 5, 200, num) 36 player1.join() 37 num += 1 38 player2 = player("Lucy", 10, 150, num) 39 player2.join() 40 if num == 2: 41 break 42 player1.artte()
#看了老师重写
1 class player(): 2 number = 0 3 4 def __init__(self, name, art, hp): 5 self.name = name 6 self.art = art 7 self.hp = hp 8 player.number += 1 9 print("新玩家%s加入游戏,当前游戏玩家数为%d" 10 % (self.name, player.number)) 11 12 def artt(self, p): 13 p.hp -= self.art 14 print("玩家%s受到玩家%s的%d点伤害,剩余hp为%d" 15 % (p.name, self.name, self.art, p.hp)) 16 p1 = player("Tom", 5, 200) 17 p2 = player("Lucy", 10, 150) 18 p2.artt(p1) 19 p2.artt(p1) 20 p1.artt(p2) 21 p1.artt(p2)