将字符串与用户输入进行比较-类问题?

时间:2019-07-14 18:03:07

标签: python class

好的,所以我在比较用户输入的值和已经打印到命令行中的值时遇到问题。

打印到命令行是由所调用的类控制的,我认为这会导致进程问题!

非常确定我需要使用 def ,但是我不熟悉所有这些,所以我不知道从哪里开始!

感谢您的任何帮助!不确定这是否是制作纸牌游戏的最佳方法,因为它非常复杂,请告诉我您的想法!

最初尝试将纸牌(bob.showHand)的手转换为字符串,但是由于它是由类生成的,因此不起作用?代码不多,所以我将其全部丢弃,希望没事!

print('Running')
import random

class Turn:
    def __init__(self, start):
        self.start = start

class Card:
    def __init__(self, suit, val):
        self.suit = suit
        self.value = val

    def show(self):
        print('{} of {}'.format(self.value, self.suit))

class Deck:
    def __init__(self):
        self.cards = []
        self.build()

    def build(self):
         for s in ['Spades', 'Clubs', 'Diamonds', 'Hearts']:
            for v in range(1,14):
                self.cards.append(Card(s, v))
    def show(self):
        for c in self.cards:
            c.show()

    def shuffle(self):
        for i in range(len(self.cards) - 1, 0, -1):
            r = random.randint(0, i)
            self.cards[i], self.cards[r] = self.cards[r], self.cards[i]
    def drawCard(self):
        return self.cards.pop()

    def draw(self, deck):
        self.hand.append(deck,drawCard())
        return self
class Player1:
    def __init__(self, name):
        self.name = name
        self.hand = []


    def draw(self, deck):
        self.hand.append(deck.drawCard())
        return self

    def showHand(self):
        for card in self.hand:
            card.show()

class Player2:
    def __init__(self, name):
        self.name = name
        self.hand = []

    def draw(self, deck):
        self.hand.append(deck.drawCard())
        return self
    def showHand(self):
        for card in self.hand:
            card.show()


print('generating deck')
deck = Deck()

print('shuffling deck')
deck.shuffle()

bob = Player1('bob')
ryan = Player2('ryan')


print('dealing deck')
for x in range(7):
bob.draw(deck)

for y in range(7):
    ryan.draw(deck)

Turn.start = 0
if Turn.start == 0:
    print('bob to act')
    print("bob's hand below")
    print(bob.showHand())

cardplay = input('Type the card to play it')

if cardplay == bob.showHand():
     print('playing card!')

我希望纸牌的印刷出现。但是,无论我尝试多少,我似乎都无法正常工作!

2 个答案:

答案 0 :(得分:0)

您的num = [] rem = 0 numinput = int(input("Enter a number : ")) for i in range (0,6): rem = numinput%10 div = int(numinput/10) if(rem != 0): num.append(rem) else: print("Loop Break.") break numinput = div num.reverse() print(thou(),hundred(),tens(),unit()) 方法不会创建字符串。它导致showHand用于在终端中显示文本。 print从用户读取一个字符串,并将其存储。 input比较永远不会比较相等,因为一侧有一个字符串,而另一侧有特殊值==

您不应让任何类的方法进行任何None调用。而是给它们print适当的字符串,然后从外面return对其进行

此外,通过为这些方法使用特殊的名称-print-我们可以使Python在__str__创建类实例时或在使用print转换为字符串时自动使用它。考虑示例:

str

答案 1 :(得分:0)

您的问题是if cardplay == bob.showHand():实际上并未按照您的想法行事。 showHand()返回None,它将永远不等于任何输入Card。您需要将用户输入的内容转换为卡,然后将该卡与Player当前持有的卡进行比较。

要解决这些问题并使代码执行您想做的事情,请将Card类更改为以下内容:

class Card:
    def __init__(self, suit, val):
        self.suit = suit
        self.value = val

    def show(self):
        print('{} of {}'.format(self.value, self.suit))

    def __eq__(self, other):
        if (other.suit == self.suit and other.value == self.value):
            return True
        else:
            return False

以及最后两行:

Turn.start = 0
if Turn.start == 0:
    print('bob to act')
    print("bob's hand below")
    bob.showHand()

cardplay = input('Type the card to play it')
picked = Card(cardplay.split()[2], int(cardplay.split()[0]))

for card in bob.hand:
    if picked == card:
        print('playing card!')

然后,您可以在输入行上完全按输入的方式输入Card完全,它将打印

'playing card!'