аватар question@mail.ru · 01.01.1970 03:00

Random text on Python

How to make a random text on Python? So that he displays only 1 variable out of two? For example, I take two variables with the following value:

 Import Randomrole1 = "" Peaceful resident "" role2 = "" mafia "" role = randon.randomint (Role1, Role2) print ("" You - "" " +" " Role) input ("" "" "" "" "" "" and I need only one variable to be displayed during the output of the text. Moreover, for every time - different, then Role1, then Role2, then again Role1. 

I hope, clearly explained. I am a beginner in Python and I ask you to answer more details.

аватар answer@mail.ru · 01.01.1970 03:00

A random element from the sequence

uses cryptographic-insufficient PG in python, so if you use it in context, where it is not desirable to guess the result of consecutive calls, for example, when generating passwords, then CSpg should be used such as random.syStemrandom () , which uses, which in tu uses the possibilities provided by OS:

  #!/usr/bin/env python3    import  randomrandom = random.syStemrandom (). Choice (" World A resident "" ,  "" mafia "" ])     

see also :. secrets The module is available with Python 3.6 - its method is just for the systemrandom (). Choice shown above.

every time a different line

so that you could print with each start of the program. Different values ​​from a given sequence, you can create an endless iterator using, which in a circle retus elements from this sequence. To maintain the condition of the iterator between the launches of the program, so that the next value is retued each time, you can use it for serialization:

  #!/Usr/bin/bin/bin/bin/bin/bin/bin/bin/bin/bin/bin/ep. python3      idertools  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  # Create an infinite itrator lieTats values ​​  it = itertools.cycle (, })   else :  #  note:  it If you canit trist it.pickle's concentent   it it is pickle.loads (Data)  # print next item    print  (#### Save items  path.write_bytes (pickle.dumps (it)) # xxx ignore data corruption Issues      

to avoid (uninhabited) Damage to the file (for example, if the food suddenly disappears during the file recording), you can use a temporary file. See.

Latest

Similar