Python 人机猜拳练习笔记

import random
# print("1.曹操2.孙权3.刘备")
# class Person:
#     def __init__(self,jue):
#         while True:
#             self.jue = int(input("请选择角色序号:"))
#             if self.jue==1:
#                 self.jue="曹操"
#                 break
#             elif self.jue==2:
#                 self.jue = "孙权"
#                 break
#             elif self.jue==3:
#                 self.jue = "刘备"
#                 break
#             else:
#                 print("输入有错误,请重新输入")
#     def caiquan(self):
#         while True:
#             global quan
#             quan=int(input("请出拳(选择序号):1.剪刀2.石头3."))
#             if quan==1:
#                 print("{0}的出拳--剪刀".format(self.jue))
#                 break
#             elif quan==2:
#                 print("{0}的出拳--石头".format(self.jue))
#                 break
#             elif quan==3:
#                 print("{0}的出拳--".format(self.jue))
#                 break
#             else:
#                 print("输入有错误,请重新输入")
# cai=Person(1)
# cai.caiquan()
# class Computer:
#     def __init__(self,dc):
#         self.dc="电脑"
#     def dquan(self):
#         global dq
#         dq=random.randint(1,3)
#         if dq==1:
#             print("{0}的出拳--剪刀".format(self.dc))
#         elif dq==2:
#             print("{0}的出拳--石头".format(self.dc))
#         else :
#             print("{0}的出拳--".format(self.dc))
# dcai=Computer(1)
# dcai.dquan()
# ying = 0
# shu = 0
# ping = 0
# class End:
#     def bi(self):
#         while True:
#             if (quan==1 and dq==1) or (quan==2 and dq==2) or (quan==3 and dq==3):
#                 global ping
#                 ping+=1
#                 print("平局")
#             elif(quan==1 and dq==3) or(quan==2 and dq==1) or(quan==3 and dq==2):
#                 global ying
#                 ying+=1
#                 print("{0}获胜".format(self.jue))
#             else:
#                 global shu
#                 shu+=1
#                 print("{0}获胜".format(self.dc))
#             a=input("是否继续y/n ")
#             if a.lower() == 'n':
#                 break
#         print("%s赢了%d"%(self.jue,ying))
#         print("%s赢了%d"%(self.dc,shu))
#         print("%d"%ping)
# vs=End()
# vs.bi()
class Person:
#属性:角色名,分数
#方法:猜拳
    def _init_(self,name=None,score=0):
        self.name=name
        self.score=score
#猜拳方法
    def chuquan(self):
        tag=None#为了扩大作用域,将此变量声明在循环外面
        while True:
            tag=input("请出拳(选择序号):1.剪刀2.石头3.")
            if tag.isdigit():
                tag=int(tag)
                if tag==1:
                    print("您的出拳--剪刀")
                    break
                elif tag==2:
                    print("您的出拳--石头")
                    break
                elif tag == 2:
                    print("您的出拳--方法")
                    break
                else:
                    print("输入错误")
            else:
                print("只能输入数字")
        return tag#
    def juese(self):
        while True:
            print("1.曹操2.孙权3.刘备")
            a=input("请选择角色序号:")
            if a.isdigit():
                a=int(a)
                if a==1:
                    print("您选的角色为:曹操")
                    self.jue = "曹操"
                    break
                elif a==2:
                    print("您选的角色为:孙权")
                    self.jue = "孙权"
                    break
                elif a==3:
                    print("您选的角色为:刘备")
                    self.jue = "刘备"
                    break
class Computer:
    name="电脑"
    score=0#得分
#电脑出拳方法
    def dchuquan(self):
        rd=random.randint(1,3)
        if rd==1:
            print("您的出拳--剪刀")
        elif rd==2:
            print("您的出拳--石头")
        else:
            print("您的出拳--")
        return rd
# import Person
# import Computer
class Game:
#属性:总局数
    count=0
    self.person=Person()
    self.computer=Computer()
    @staticmethod
    def sgame():
        print("人机猜拳")
        person.juese()
        p=person.chuquan()
        c=computer.dchuquan()
    def res(self,p,s):
        if(p==1 and c==3) or(p==2 and c==1)or(p==3 and c==2):
            person.score+=1
            print("恭喜{0}赢了".format(person.name))
        elif p==c:
            self.count+=1
            print("平局")
        else:
            computer.score+=1
            print("电脑硬")
#本局得分
    def res(self,p,s):...
    def vs(self):
        print("{0}vs电脑".format(person.name))
        print("{0}赢了{1}".format(person.name,person.score))
        print("{0}".format(self.count))
game=Game()
game.sgame()
#妈卖批,做个粑粑的笔记

你可能感兴趣的:(Python 人机猜拳练习笔记)