Python 2.7 使用字典选择游戏中的场景,但它';这不是正确的场景。为什么?
开始时,如果一个随机整数=1,它将播放一个随机事件。应从包含每个场景名称的字典中选择随机事件(也是随机的)。相反,它不会尝试访问其他场景。它只按照所写的顺序运行每个方法(函数?)。我做错了什么?这是一个糟糕的方式吗 我还在学习,所以任何指点都很感激Python 2.7 使用字典选择游戏中的场景,但它';这不是正确的场景。为什么?,python-2.7,Python 2.7,开始时,如果一个随机整数=1,它将播放一个随机事件。应从包含每个场景名称的字典中选择随机事件(也是随机的)。相反,它不会尝试访问其他场景。它只按照所写的顺序运行每个方法(函数?)。我做错了什么?这是一个糟糕的方式吗 我还在学习,所以任何指点都很感激 from random import randint from time import sleep class RandomEvent(object): def __init__(self, wallet): self.w
from random import randint
from time import sleep
class RandomEvent(object):
def __init__(self, wallet):
self.wallet = wallet
def old_man(self):
#insert art
print "\nExcuse me, I have an unusual request."
print "My horoscope tells me it's my lucky day,"
print "but I don't have any money."
print "\nIf you'll let me borrow some money, I"
print "promise to give you half of all the winnings."
print "Do we have a deal?"
give_money = raw_input("(y)es or (n)o? ")
if give_money.lower() == "y":
print "\nFunds: $", self.wallet
how_much = int(raw_input("How much money will you give? $"))
if how_much <= self.wallet:
how_much *= randint(2,3)
print "Haha! I won $%d! Here's your share of the money, kiddo." % (how_much*2)
self.wallet += how_much
else:
print "Eh, kids these days... no faith in their elders... (grumble grumble)..."
sleep(2)
print "\nA few moments later you see the old man hit the jackpot at a slot machine."
return self.wallet
def robber(self):
#insert art
print "\nPsssst! Hey, you!"
#sleep(1)
print "\nYou glance down to see a knife discreetly placed"
print "to your stomach."
#sleep(1)
print "\nHand over all your cash. No funny business."
print "\nFunds: $", self.wallet
how_much = int(raw_input("How much money will you give? $"))
lie_success = randint(1,3)
if how_much == self.wallet:
self.wallet = 0
print "\nNice doin' business with ya."
print "The robber quickly disappears into a nearby crowd,"
print "taking all your money with him."
if how_much != self.wallet and lie_success == 1:
self.wallet -= how_much
print "\nNice doin' business with ya."
print "The robber quickly disappears into a nearby crowd,"
print "taking your money with him."
else:
print "\nYou think you're a wise guy, eh?"
sleep(2)
print "\nYou are dead. GAME OVER"
sleep(2)
exit()
return self.wallet
def pretty_woman(self):
pass
###----------------------GAME CODE BELOW---------------
funds = 500
encounter = 1
while True:
if encounter == randint(1, 1): # 1,1 FOR TESTING
story = RandomEvent(funds)
scene_list = {1: story.old_man(), 2: story.robber(), 3: story.pretty_woman()}
funds = scene_list[2] #randint(1,3)] FOR TESTING
else:
print "\n\n\nWelcome to Dreams Casino."
print "Where would you like to go?"
print "(1) Slot Machines"
print "(2) Roulette Table"
print "(3) Leave the Casino"
print "\nFunds: $", funds
game_choice = int(raw_input("> "))
来自随机导入randint
从时间上导入睡眠
类随机事件(对象):
def uuu init uuuu(自我,钱包):
钱包
def老人(自我):
#插入艺术
打印“\n对不起,我有一个不寻常的请求。”
打印“我的星座告诉我这是我的幸运日,”
打印“但我没有钱。”
打印“\n如果你让我借点钱,我”
打印“承诺给你一半的奖金。”
打印“我们达成协议了吗?”
给钱=原始输入(“(y)es或(n)o?”)
如果给你钱。下()=“y”:
打印“\n资金:$”,self.wallet
how_mound=int(原始输入(“你会给多少钱?$”)
如果在创建字典时调用了多少函数:
scene_list = {1: story.old_man(), 2: story.robber(), 3: story.pretty_woman()}
由于希望场景列表[1]
引用函数,请传递函数:
scene_list = {1: story.old_man, 2: story.robber, 3: story.pretty_woman}
我明白了,“()”应该放在我想调用函数的地方?ie在“场景列表[2]之后”