简体   繁体   English

python 程序没有给出结果

[英]The python program is giving no result

I am trying to code a card game in python using OOP. the user should select either red( hearts and diamonds) or black( clubs and spades) the user then goes on with the game.我正在尝试使用 OOP 在 python 中编写纸牌游戏。用户应该 select 红色(红心和方块)或黑色(梅花和黑桃)然后用户继续玩游戏。

here is the code:这是代码:

import random

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

    def print_card(self):
        print(self.name + ' of ' + self.suit)

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

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

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

    def show(self):
        for c in self.cards:
            c.show()


class Game:
    def __init__(self, player, score):
        self.player = player
        self.score = score
        self.cards = []
        self.hand = []

    def shuffle(self):
        random.shuffle(self.cards)

    def drawCard(self):
        return self.cards.pop()

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

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

    def reset(self):
        pass
print('Welcome message')
user = input("Please enter your name: ")
print('While you start your score is 1000')
team = input((' RED or BLACK  \n Your team : '))
while True:
    if team == 'red':
        print('Great, You have chosen Team Red, hearts and diamonds will fetch you points, clubs and sp')
        print("")
        playGame = input(('Press ENTER to play'))
        print('Game starts')
        shuffleCard = input(('Press ENTER to shuffle and Pick your card'))

        deck = Deck()
        deck.shuffle()

        print('That was a good mix')

        showCard = input(('Press ENTER to reveal your card'))
        user = Game(user, 1000)
        user.showHand --> here i am expecting the final card to show up but its not happening 

the terminal gives this error:终端给出了这个错误:

Traceback (most recent call last):
  File "/Users/yoshithkotla/PycharmProjects/pythonFinalProject001/main.py", line 71, in <module>
    player = player()
TypeError: 'str' object is not callable

Details of the problem: Using the OOP topics and programs we examined in our Lecture Notes 8 and 9 develop a Python program to implement a card game.问题详情:使用我们在讲义 8 和 9 中检查过的 OOP 主题和程序开发一个 Python 程序来实现纸牌游戏。 You select the card game.你 select 纸牌游戏。 o You can invent your own simple card game; o 你可以发明自己的简单纸牌游戏; just provide the instructions for your card game as comments in your Python program.只需在您的 Python 程序中提供纸牌游戏的说明作为注释即可。 In our Lecture Notes 9, we will be developing OOP Python code for the fundamentals of a card game.在我们的第 9 讲义中,我们将为纸牌游戏的基础开发 OOP Python 代码。 Use this code in your implementation of your card game.在纸牌游戏的实现中使用此代码。 Add additional OOP code for your specific card game.为您的特定纸牌游戏添加额外的 OOP 代码。

Changes made:- player=player() -> player=game()所做的更改:- player=player() -> player=game()

Code:-代码:-

class game:
    def __init__(self, player, score):
        self.player = player
        self.score = score

    def start_game(self, player, score, team):
        self.score = score
        self.player = player
        self.team = team

print("Welcome")
player = input("Please enter your name: ")
print('While you start your score is 1000')
team = input((' RED or BLACK  \n Your team : '))
while team == 'red':
    print('Great, You have chosen Team Red, hearts and diamonds will fetch you points, clubs and sp')
    print("")
    playGame = input(('Press ENTER to play'))
    print('game starts')
    shuffleCard = input(('Press ENTER to shuffle and Pick your card'))
    deck = Deck()
    deck.shuffle()
    print(' that was a good mix')
    showCard = input(('Press ENTER to reveal your card'))
    player = game()
    player.draw(deck)
    player.showHand
    break

Output:- Output:-

Welcome
Please enter your name: Yash
While you start your score is 1000
 RED or BLACK  
 Your team : red
 Great, You have chosen Team Red, hearts and diamonds will fetch you points, clubs and sp

Press ENTER to play

Updated query:-更新查询:-

I think you should go with the process to learn all the conditions for the game than start your code.. for reference you should follow this -> click here to know all the conditions for the game and then try to code..!我认为你应该 go 学习游戏的所有条件而不是开始你的代码.. 作为参考,你应该遵循这个 -> 单击此处了解游戏的所有条件然后尝试编码..!

In the line player = player() you are trying to call a function named 'player' by adding brackets.player = player()行中,您尝试通过添加括号来调用名为“player”的 function。 Player is not a function but a string variable where you store the entered name of a player, so you cannot call it. Player 不是 function 而是一个字符串变量,您可以在其中存储输入的玩家名称,因此您无法调用它。 Try to erase the brackets or delete line completely (since you've already assigned the variable in line player = input("Please enter your name: ") ).尝试完全删除括号或删除行(因为您已经在行player = input("Please enter your name: ")中分配了变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM