Python



  • How do you make an early text on Python? So he can only get one variable out of two? For example, I take two variables with that meaning:

    import random
    

    role1 = "People"
    role2 = Mafia
    role = randon.randomint(role1, role2)

    print("You- + role)

    input(")

    And I need the text to come out when it's done. Every time, different, the role1, the role2, the role1 again.

    I hope I explained. I'm a rookie at Python, and I'm asking you to answer the details.



  • Accuracy

    https://docs.python.org/3/library/random.html#random.choice Uses a cryptographically insecure PRNG in Pitton, so if you use it in a context where it is not desirable that the result of consistent challenges, such as password generation, should be used as a CSPRNG. random.SystemRandom()who uses https://docs.python.org/3/library/os.html#os.urandom which in turn uses the OS facilities:

    #!/usr/bin/env python3
    import random
    

    random_item = random.SystemRandom().choice(["Мирный житель", "Мафия"])

    See also: https://www.python.org/dev/peps/pep-0506/ ♪

    Every time different (non-radial) lines

    In order for each programme launch to print different values from the intended sequence, an endless terator may be created using https://docs.python.org/3/library/itertools.html#itertools.cycle which brings elements from this sequence in circles. In order to keep the heterator between the launches of the programme so that the next value can be returned each time, it can be used https://docs.python.org/3/library/pickle.html for the show:

    #!/usr/bin/env python3
    """Print a different string on each run in a loop."""
    import itertools
    import pathlib
    import pickle

    load items

    path = pathlib.Path('it.pickle')
    try:
    data = path.read_bytes() # XXX no file locking, ignore concurrent issues
    except FileNotFoundError: # 1st run
    # create an infinite iterator that repeats the values
    it = itertools.cycle(iter({"Мирный житель", "Мафия"}))
    else:
    # NOTE: it is insecure if you can't trust it.pickle's content
    it = pickle.loads(data)

    print next item

    print(next(it))

    save items

    path.write_bytes(pickle.dumps(it)) # XXX ignore data corruption issues

    A temporary file can be used to avoid (movable) damage to the file (e.g. if the food suddenly falls off during the record). See https://stackoverflow.com/a/12012813/4279 ♪




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2