There is such a code for a simple toy:

from time import sleep from random import choice from random import randrange ee_names = [ "Злая крыса", "Не очень-то злой филин", "Чуть озверевший голубь", "Простуженная улитка" ] hp = randrange(3,10) ename = choice(ee_names) list_ = [ (ename, hp), (ename, hp), (ename, hp), ] name, hp = choice(list_) for x in range(5): print( "[",name,"]\nHP: "+str(hp) ) sleep(1) 

The problem is that each time the same name is displayed with the same parameters ( hp ). How can I update the counter? In C ++ , I remember, it was srand (time (0)) .

    3 answers 3

    The results are the same because you form them not in a cycle, but before it. It is not clear from the task why you need list_ , so you can do this:

     from time import sleep from random import choice, randrange ee_names = [ 'Злая крыса', 'Не очень-то злой филин', 'Чуть озверевший голубь', 'Простуженная улитка' ] for _ in range(5): name = choice(ee_names) hp = randrange(3, 10) print('[%s]\nHP: %s' % (name, hp)) sleep(1) 

    During the execution, five hits are delivered, the attacking NPC and the damage is chosen in a pseudo-random manner.

      randrange() returns an integer. hp = randrange(3,10) assigns the name hp to this return value. The repetition of this name in the program does not change this integer and does not force hp to refer to any other number.

      Similarly, choice(ee_names) returns a string. ename = choice(ee_names) assigns the name ename to this string. Repetition of this name in the program does not change this line and does not force the ename to refer to any other string.

      That is, all elements in the list_ list are equal to each other and therefore choice(list_) always returns equivalent values ​​in this case.

      hp does not change inside the loop, so all iterations show the same value.

      Moreover, integers and strings are immutable in Python, the only way to "change" hp is to assign a new value (usually = sign is used to make the name point to a new object, but it is not the only way, for example, import associates various objects from other models with the names in the current module).

      I recommend to the pictures next to the heading "In Python there are" names "" from "Code Like a Pythonista: Idiomatic Python" by David Goodger and look at the presentation of Ned Batchelder from Pycon 2015, where this question is described in more detail (code and pictures on slides) to understand the relationship between names and their meanings in Python.

      Even if the hp name would refer to a function that generates a new value, then there are not enough parentheses () to call it. In some languages, the use of brackets may be optional, but in Python brackets are required:

       #!/usr/bin/env python # -*- coding: utf-8 -*- import time import random from functools import partial ee_names = [ u'Злая крыса', u'Не очень-то злой филин', u'Чуть озверевший голубь', u'Простуженная улитка' ] generate_name = partial(random.choice, ee_names) generate_hp = partial(random.randrange, 3, 10) for _ in range(5): print(u"[{name}]\n\tHP: {hp}".format(name=generate_name(), hp=generate_hp())) time.sleep(1) 

      partial() simply remembers the arguments given to it and uses them on subsequent calls.

      Here it is better to insert the implementation of functions directly in place, as shown in the answer @idle sign .

        In Python, the random.seed function is used for this:

         import random from datetime import datetime random.seed(datetime.now()) 
        • It does not work: everything also gives the same values. - CockLobster
        • are the same with different runs of the script or are you talking about the values ​​that are displayed in the loop? - dizballanze