Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python random.randint函数没有按我希望的方式工作_Python_List_Python 2.7_Python 3.x - Fatal编程技术网

Python random.randint函数没有按我希望的方式工作

Python random.randint函数没有按我希望的方式工作,python,list,python-2.7,python-3.x,Python,List,Python 2.7,Python 3.x,每当我运行这个程序时(不是一个严肃的程序,只是为了弄清楚事情的来龙去脉),怪物(怪物[1])造成的伤害只是一次又一次地重复相同的数字。它随机化一次,然后重复它。我明白发生了什么,但我不明白为什么。这是代码(很抱歉,我不知道我的问题是否清楚…在我的头脑中有意义lol) 编辑-我希望monster[1]函数每次都随机化,但我不知道如何在不编写更多代码的情况下做到这一点(即damage=random.randint(5,15)health-=damage)。如果我这样写,效果很好。但是如果我有一系列1

每当我运行这个程序时(不是一个严肃的程序,只是为了弄清楚事情的来龙去脉),怪物(怪物[1])造成的伤害只是一次又一次地重复相同的数字。它随机化一次,然后重复它。我明白发生了什么,但我不明白为什么。这是代码(很抱歉,我不知道我的问题是否清楚…在我的头脑中有意义lol)

编辑-我希望monster[1]函数每次都随机化,但我不知道如何在不编写更多代码的情况下做到这一点(即
damage=random.randint(5,15)
health-=damage
)。如果我这样写,效果很好。但是如果我有一系列10种不同的怪物,我不想特别写这些。我只想拜访每一个怪物,让它自己运行,如果这有意义的话

编辑#2:由于元组是不可变的(即
monster=[100,(random.randint(5,15)),random.randint(15,30)],
这段代码不起作用,我已经尝试过了,只是想知道它是否可行

import random
monster = [100, random.randint(5, 15), random.randint(15, 30)]
health = 200
a = 1

print "you run into a monster"
while a == 1:
    if monster[0] <= 0:
        print "monster is dead"
        print "you get, " + str(monster[1]) + " exp"
        a += 1
    elif monster[0] >= 0:
        att = random.randint(5, 15)
        monster[0] -= att
        health -= monster[1]
        print ("you attack the monster and do, " + str(att) + " damage"
               " the monster does," + str(monster[1]) + " damage to you")
        print "you have, " + str(health) + " health left"
        print "monster has, " + str(monster[0]) + " health left"
随机导入
怪物=[100,random.randint(5,15),random.randint(15,30)]
健康=200
a=1
打印“你遇到怪物”
当a==1时:
如果怪物[0]=0:
att=random.randint(5,15)
怪物[0]=att
生命-=怪物[1]
打印(“你攻击怪物并做,”+str(att)+“伤害”
“怪物会,”+str(怪物[1])+“对你造成伤害”)
打印“您有,”+str(健康)+“健康剩余”
打印“怪物拥有”+str(怪物[0])+“生命值剩余”
此行:

monster=[100,random.randint(5, 15), random.randint(15, 30)]
一次保存
100
和两个随机数。例如,这可能是运行时
monster
的表示形式:

monster=[100, 11, 26]

这些数字不会改变,因为赋值只发生一次(例如循环外)

monster=[100,random.randint(5,15),random.randint(15,30)]

定义此列表时,整个列表都是相同的。值不会在每次执行
monster[1]
时更改

要解决此问题,您可以执行以下操作:

monster=[100, random.randint, random.randint]
当你去叫它的时候:

monster[1](15, 15)

只是一个选项。虽然字典在这里可能更好。

monster[1]
在代码的第二行分配一个随机整数。然后它作为一个整数存储在列表中。该整数将保持不变。您必须重新运行
random.randint(5,15)
调用以获取新的“损坏”编号或新经验金额

虽然我认为这可能超出了您正在研究的范围,但您可能希望创建一个
怪物,而不是使用数组,然后使用一种方法在每次攻击时生成这些随机整数

下面是一个怪物类的例子,可以让你了解它是如何工作的

class monster():
    def __init__(self,health):
        self.health = health

    def attack(self):
        return random.randint(5,15)
然后你可以创建一个新的怪物,如下所示

monster = monster(100)
monsterDamage = monster.attack()
health -= monsterDamage
print "The monster does " + str(monsterDamage) + " damage to you."
att = random.randint(5,15)
monster.health -= att
看看它会造成多大的伤害,如下所示

monster = monster(100)
monsterDamage = monster.attack()
health -= monsterDamage
print "The monster does " + str(monsterDamage) + " damage to you."
att = random.randint(5,15)
monster.health -= att
并降低怪物的生命值,如下所示

monster = monster(100)
monsterDamage = monster.attack()
health -= monsterDamage
print "The monster does " + str(monsterDamage) + " damage to you."
att = random.randint(5,15)
monster.health -= att

那么,您只在第二行中给了
monster[1]
一次值:

monster=[100,random.randint(5, 15), random.randint(15, 30)]

该行只执行一次,所以“当然”怪物[1]
总是一样的。如果您想
怪物[1]
(和/或
怪物[2]
)若要更改,您需要在循环中为它指定一个新值。您这样做是为了
att
!您需要对所有其他您希望看到更改的内容执行类似操作:-)

当您拨打电话时:

monster = [100, random.randint(5, 15), ...]
执行函数random.randint(5,15),并在列表中存储特定值

我将提供另一种方法,通过将函数存储为字符串来执行您想要的操作;您可以编写:

monster = [100, "random.randint(5, 15)", ...]
然后,当您想要获得一个新的随机整数时,调用:

r=eval(monster[1])
每次调用它时都会得到一个不同的整数,因为函数会被重新计算

(请注意,如果要重复使用随机值,例如打印随机值,则必须将其存储在临时变量“r”中,然后使用str(r),而不是str(eval(monster[1]),因为前一种方法打印存储在“r”中的调用结果,后一种方法再次重新计算函数)

例如


@DHandle我仍在学习python我还没有学习过课程。@user2423223可以理解!请随意提问,如果你想了解更多,你可以参考这篇文章。谢谢你的参考资料。我从一本书和一些他们不太详细的东西中学习。python.org网站确实是一个很好的参考易于阅读,并通过附加阅读和示例非常简单地展示所有内容,以便在您想了解更多信息时“深入了解”。@user2423223我添加了一些示例类代码,以帮助您入门。