我不知道某些行的优先级OOP

时间:2018-04-24 12:21:17

标签: python python-3.x oop

我的代码有问题我不知道某些行的优先级,

我想制作(游戏猜数字), 用户和计算机播放器都有(10)分, 所以用户输入数字形式为1到2,如果(用户输入)==(猜测次数), 从计算机点(10)= 9将-1,反之亦然, 所以我的问题在点,在第一次用户输入数字后,它变为自动(10), 抱歉我的英语太糟糕了。 在我的代码下面,我希望有人帮助我,谢谢你。

from random import randint as any_number

class Game():
    def __init__(self, name, number, points):
        self.name = name
        self.number = number
        self.points = points

    def score(self, other):
        guess = any_number(1, 2)
        if self.number == guess:
            print('{} your are correct'.format(self.name))
            other.points = other.points - 1
        else:
            print('{} your are incorrect'.format(self.name))
        print('{} points is ({}) and {} points is ({})'.format(self.name, self.points, other.name, other.points))


while True:
    user1 = int(input('Guess a number from 1 to 2: '))
    computer = any_number(1, 2)

    p1 = Game('Player1', user1, 10)       #  <---- my problem here 
    p2 = Game('player2', computer, 10)    #  <---- my problem here

    if type(user1) == int:
        p1.score(p2)
        print('================================================= ')
        p2.score(p1)
    else:
        print('your input is Error')

1 个答案:

答案 0 :(得分:0)

如果我很好理解你的问题,问题是你在每个回合都在创建一个类型为Game的新实例,所以试试这个:

from random import randint as any_number

class Game():
    def __init__(self, name, number, points):
        self.name = name
        self.number = number
        self.points = points

    def score(self, other):
        guess = any_number(1, 2)
        if self.number == guess:
            print('{} your are correct'.format(self.name))
            other.points = other.points - 1
        else:
            print('{} your are incorrect'.format(self.name))
        print('{} points is ({}) and {} points is ({})'.format(self.name, self.points, other.name, other.points))

p1 = Game('Player1', user1, 10)
p2 = Game('player2', computer, 10) 

while True:
    user1 = int(input('Guess a number from 1 to 2: '))
    computer = any_number(1, 2)

    if type(user1) == int:
        p1.score(p2)
        print('================================================= ')
        p2.score(p1)
    else:
        print('your input is Error')