Python基本*没有运行?
问题描述:
所以我花了一天左右的时间在这个*程序上做一个学校任务,出于某种原因,当我运行该程序时唯一会发生的事情是它将打印函数中的第一条语句,但随后关闭脚本。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()
我有点在这方面的损失,所以任何帮助将不胜感激!
答
在接下来的行看来,你想调用方法,但实际上你没有。
SlotMachine.playRound
既然playRound是你可以从大气观察对象调用此类
gaw.playRound()
我希望这将解决您的问题实例方法。
答
当您为类创建实例时,您应该只使用该实例调用该函数。 我在我的代码中所做的更改是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()
尝试'SlotMachine.playRound()' –