'''
3.人和机器猜拳游戏写成一个类,有如下几个函数:
1)函数1:选择角色1 曹操 2张飞 3 刘备
2)函数2:角色猜拳1剪刀 2石头 3布 玩家输入一个1-3的数字
3)函数3:电脑出拳 随机产生1个1-3的数字,提示电脑出拳结果
4)函数4:角色和机器出拳对战,对战结束后,最后出示本局对战结果...赢...输,然后提示用户是否继续?按y继续,按n退出。
5)最后结束的时候输出结果 角色赢几局 电脑赢几局,平局几次 游戏结束
'''
第一种方法:
class ren_VS_jiqi:
def role_choice(self):
self.roles = {1:'曹操',2:'张飞',3:'刘备'}
self.role_num = int(input('请选择角色1 曹操 2张飞 3 刘备:'))
return self.roles[self.role_num]
def role_punch(self):
# self.role = self.role_choice()
while True:
punch = {'1': '剪刀', '2': '石头', '3': '布'}
role_punch1 = input('请猜拳1剪刀 2石头 3布:')
if role_punch1 in punch.keys():
print('%s出的拳是:%s' %(self.roles[self.role_num],punch[role_punch1]))
break
else:
print('输入有误,请重新输入!')
continue
return role_punch1
def machine_punch(self):
import random
punch1 = {'1': '剪刀', '2': '石头', '3': '布'}
machine_punch = str(random.randint(1,3))
print("机器出拳:%s" % punch1[machine_punch])
return machine_punch
def R_Vs_M(self):
role = self.role_choice()
result_reny = 0 #人赢
result_jiqiy = 0 #机器赢
result_ping = 0 #平局
while True:
ren_num = int(self.role_punch())
jiqi_num = int(self.machine_punch())
if ren_num != jiqi_num:
if ren_num == 1 and jiqi_num ==3:
result_reny +=1
elif ren_num == 2 and jiqi_num == 1:
result_reny +=1
elif ren_num == 3 and jiqi_num == 1:
result_reny +=1
else:
result_jiqiy +=1
else:
result_ping +=1
choice=input("是否要继续猜拳?按y继续,按n退出")
if choice=='y':
continue
else:
break
print("对战结束:%s赢了%s局,电脑赢了%s局,平了%s局"%(self.roles[self.role_num],result_reny,result_jiqiy,result_ping))
ren_VS_jiqi().R_Vs_M()
第二种方法:
class HumanVSMachine: def choose_role(self): while True: role_info={"1":"曹操","2":"张飞","3":"刘备"} role_num=input("请选择你喜欢的角色:1:曹操 2:张飞 3:刘备") if role_num in role_info.keys(): print("你选择的角色是%s"%role_info[role_num]) break else: print("角色选择错误,请重新选择!") continue #返回一个值 返回角色值 return role_info[role_num] #方法一: def cq(self,role,mode):#mode=1 人出拳 mode=2 就是电脑出拳 import random cq_info={"1":"石头","2":"剪刀","3":"布"} if mode==1: cq_num=input("请输入对应的数字出拳:1石头 2剪刀 3布") elif mode==2: cq_num=str(random.randint(1,3)) if cq_num in cq_info.keys(): print(role+"出的是%s"%cq_info[cq_num]) else: print("出拳错误!") return cq_num # 方法二: # def role_cq(self,role_name): # cq_num=input("请输入对应的数字出拳:1剪刀 2石头 3布") # cq_info={"1":"剪刀","2":"石头","3":"布"} # if cq_num in cq_info.keys(): # print(role_name+",你出的是%s"%cq_info[cq_num]) # else: # print("出拳错误!") # return cq_num # # def machine_cq(self): # cq_num=str(random.randint(1,3))#需要转换一下格式 # cq_info={"1":"剪刀","2":"石头","3":"布"} # if cq_num in cq_info.keys(): # print("电脑出的是%s"%cq_info[cq_num]) # else: # print("出拳错误!") # return cq_num#字符串 def human_vs_machine(self): #人机对战 role=self.choose_role()#角色 #变量: human_win=0 ping=0 machine_win=0 while True: #方法一: # human_cq=int(self.role_cq(role))#角色出拳 # machine_cq=int(self.machine_cq())#机器出拳 #方法二 human_cq=int(self.cq(role,1)) machine_cq=int(self.cq("电脑",2)) if human_cq!=machine_cq:#数值比较 1 石头 2 剪刀 3布 if human_cq==1 and machine_cq==2: human_win+=1 elif human_cq==2 and machine_cq==3: human_win+=1 elif human_cq==3 and machine_cq==1: human_win+=1 else: machine_win+=1 else: ping+=1 choice=input("是否要继续猜拳?按y继续,按n退出") if choice=='y': continue else: break print("对战结束:人赢了%s局,电脑赢了%s局,平了%s局"%(human_win,machine_win,ping)) HumanVSMachine().human_vs_machine()