所以我花了一天左右的时间来完成这个老虎机程序的学校作业,出于某种原因,我运行该程序时唯一会发生的事情是它将打印函数中的第一个语句但是然后关闭脚本。
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 :(得分:0)
在python方法或函数调用语法是
[instance.]instance_method<left parenthesis> [<arguments>]><right parenthesis>
从口头上讲,这意味着,调用方法或python中的函数的语法是: - 首先可选地指定实例对象的变量(或静态方法的类名),后跟dot(。)运算符。对于功能,此步骤不是必需的。然后是方法名称,后跟左括号和右括号。可选地,在括号之间指定方法的参数 在接下来的行中,您似乎想要调用方法,但实际上您并没有。
SlotMachine.playRound
这不会调用方法调用。但只是返回instancemethod类型的对象。 由于playRound是实例方法,因此调用方法的常规方法是使用实例object.e.g gaw对象,如:
gaw.playRound()
我希望这可以解决您的问题。
答案 1 :(得分: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()