【问题标题】:The python program is giving no resultpython程序没有给出结果
【发布时间】:2022-12-17 17:52:08
【问题描述】:

我正在尝试使用 OOP 在 python 中编写纸牌游戏。 用户应选择红色(红心和钻石) 或黑色(梅花和黑桃) 然后用户继续游戏。

这是代码:

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 = player()
    player.draw(deck)
    player.showHand
    break

终端给出了这个错误:

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

【问题讨论】:

  • player 是一个字符串对象。

标签: python oop


【解决方案1】:

所做的更改:- player=player() -&gt; player=game()

代码:-

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  
 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

输出:-

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

【讨论】:

    猜你喜欢
    • 2020-06-30
    • 1970-01-01
    • 2021-01-06
    • 2023-03-30
    • 2020-11-22
    • 2018-11-30
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多