我在这里有一些代码,可用于我在Python 3.x中制作的基本游戏。如您所见,局部变量'code1'在我的值之间创建了一个随机的两位数,用于我的库的解锁代码的第一部分(在游戏中稍后)。我想做的是以某种方式存储随机整数,因此,如果重新访问特定的房间,它将显示此函数输出的第一个随机数,并且不会不断变化,因为这会破坏线索收集的目的。
def corridorOptions():
code1 = random.randint(30,60)
corridorChoice = input('> ')
if corridorChoice == "loose":
delayedPrint("You lift the loose floorboard up out its place." + '\n')
delayedPrint("It shifts with hardly any resistance." + '\n')
delayedPrint("There is a number etched. It reads " + "'" + str(code1) + "'")
干杯们。
我建议您向该corridorOptions
函数添加一个属性,该属性在函数的第一次调用中创建时仅初始化一次
from random import randint
def corridorOptions():
if not hasattr(corridorOptions, 'code'):
corridorOptions.code = randint(30, 60)
print("There is a number etched. It reads '{0:02d}'".format(corridorOptions.code))
corridorOptions()
corridorOptions()
corridorOptions()
corridorOptions()
corridorOptions()
输出
There is a number etched. It reads '58'
There is a number etched. It reads '58'
There is a number etched. It reads '58'
There is a number etched. It reads '58'
There is a number etched. It reads '58'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句