2017-11-11 225 views
0

所以我花了一天左右的時間在這個老虎機程序上做一個學校任務,出於某種原因,當我運行該程序時唯一會發生的事情是它將打印函數中的第一條語句,但隨後關閉腳本。Python基本老虎機沒有運行?

import random 

STATS = {"Win": 0, "Lose": 0} 

def playSlots(): 
    player = Player("Player") 
    game = Game(player, []) 
    print("Welcome to my not as rough slot machine, now featuring classes!") 
    gaw = SlotMachine(1,1,1) 
    SlotMachine.playRound 
    if player.money < 1: 
     print("Out of Money") 



class Player: 
    def __init__(self, name): 
     self.money = 10 

    def getMoney(self): 
     return self.money 

    def changeMoney(self, value): 
     self.credits += value 

class Game: 
    def __init__(self, player, stats): 
     self.player = player 
     self.stats = stats 

    def statChange(self, outcome): 
     global STATS 
     if outcome == "Win": 
      STATS["Win"] += 1 
     elif outcome == "Lose": 
      STATS["Lose"] += 1 

class SlotMachine: 

    def __init__(self, slotL, slotC, slotR): 
     self.slotL = 1 
     self.slotC = 1 
     self.slotR = 1 

    def randomSlots(self): 
     self.slotL = random.choice([1, 2, 3]) 
     self.slotC = random.choice([1, 2, 3]) 
     self.slotR = random.choice([1, 2, 3]) 
     return self.slotL, self.slotC, self.slotR 

    def playRound(self): 
     while Player.getMoney > 1: 
      print("You have",Player.getMoney(), "tokens") 
      playerWager = int(input("Enter the amount of money you would like to wager: ")) 
      if playerWager > Player.getMoney() or playerWager == 0: 
       print("Invalid Wager") 
       continue 
      Player.changeMoney(playerWager) 
      self.randomSlots 
      print(self.slotL, "|", self.slotC, "|", self.slotR) 
      if (self.slotL == self.slotC) and (self.slotC == self.slotR): 
       print("Win") 
       Player.changeMoney((playerWager * 2)) 
       Game.statChange("Win") 
       print("Money: ", Player.getMoney()) 
      else: 
       print("Lose") 
       Game.statChange("Lose") 
       print("Money: ", Player.getMoney()) 
       if Player.getMoney() < 1: 
        print("Out of Money") 
        break 
      userContinue = input("Continue? (q to quit): ") 
      if userContinue == "q": 
       break 

def main(): 
    playSlots() 
main() 

我有點在這方面的損失,所以任何幫助將不勝感激!

+0

嘗試'SlotMachine.playRound()' –

回答

0

在接下來的行看來,你想調用方法,但實際上你沒有。

SlotMachine.playRound 

既然playRound是你可以從大氣觀察對象調用此類

gaw.playRound() 

我希望這將解決您的問題實例方法。

0

當您爲類創建實例時,您應該只使用該實例調用該函數。 我在我的代碼中所做的更改是gaw.playRound(player,game)def playRound(self,player,game):,在playRound函數中,我們必須僅針對特定玩家使用玩家和遊戲實例。 以下代碼適用於我。一旦嘗試這一點。

import random 

STATS = {"Win": 0, "Lose": 0} 

def playSlots(): 
    player = Player("Player") 
    game = Game(player, []) 
    print("Welcome to my not as rough slot machine, now featuring classes!") 
    gaw = SlotMachine(1,1,1) 
    gaw.playRound(player,game) 
    if player.money < 1: 
     print("Out of Money") 



class Player: 
    def __init__(self, name): 
     self.money = 10 
     self.credits = 0 

    def getMoney(self): 
     return self.money 

    def changeMoney(self, value): 
     self.credits += value 

class Game: 
    def __init__(self, player, stats): 
     self.player = player 
     self.stats = stats 

    def statChange(self, outcome): 
     global STATS 
     if outcome == "Win": 
      STATS["Win"] += 1 
     elif outcome == "Lose": 
      STATS["Lose"] += 1 

class SlotMachine: 

    def __init__(self, slotL, slotC, slotR): 
     self.slotL = 1 
     self.slotC = 1 
     self.slotR = 1 

    def randomSlots(self): 
     self.slotL = random.choice([1, 2, 3]) 
     self.slotC = random.choice([1, 2, 3]) 
     self.slotR = random.choice([1, 2, 3]) 
     return self.slotL, self.slotC, self.slotR 

    def playRound(self,player,game): 
     while player.getMoney() > 1: 
      print("You have",player.getMoney(), "tokens") 
      playerWager = int(input("Enter the amount of money you would like to wager: ")) 
      if playerWager > player.getMoney() or playerWager == 0: 
       print("Invalid Wager") 
       continue 
      player.changeMoney(playerWager) 
      self.randomSlots 
      print(self.slotL, "|", self.slotC, "|", self.slotR) 
      if (self.slotL == self.slotC) and (self.slotC == self.slotR): 
       print("Win") 
       player.changeMoney((playerWager * 2)) 
       game.statChange("Win") 
       print("Money: ", player.getMoney()) 
      else: 
       print("Lose") 
       game.statChange("Lose") 
       print("Money: ", Player.getMoney()) 
       if player.getMoney() < 1: 
        print("Out of Money") 
        break 
      userContinue = input("Continue? (q to quit): ") 
      if userContinue == "q": 
       break 

def main(): 
    playSlots() 
main()