Home python Random text on Python

Random text on Python

Author

Date

Category

How to make random text on Python? So he displayed only 1 variable from two? For example, I take two variables with such a value:

Import Random
Role1 = "Mirny resident"
Role2 = "Mafia"
Role = Randon.Randomint (Role1, Role2)
Print ("You -" + Role)
INPUT ("")

and I need that only one variable is displayed when the text is displayed. And with each time – different, then Role1, then Role2, then again Role1.

Hopefully, it explained. I am a novice in Python and please answer more.


Answer 1, Authority 100%

Random element from the sequence

Random.Choice (SEQ) Uses the cryptographic-unsafe PRNG in Python, so if you use it in the context, where it is not desirable to guess the result of consecutive calls, for example, when generating passwords, you should use CSPRNG such as Random.SystemRandom () , which uses Os.urandom ( ) , which in turn uses the capabilities provided by OS:

#! / usr / bin / env python3
Import Random
Random_item = Random.SystemRandom (). Choice (["Mirny resident", "Mafia"])

See Also: PEP 0506 – adding a secrets module to the standard library .

Every time different (non-random) string

To make different values ​​from a given sequence each time you start the program, you can create an endless iterator using ITERTOOLS.CYCLE () , which in a circle returns elements from this sequence. To save the status of the iterator between the program launches so that the next value is returned each time, you can use Pickle for serialization:

#! / 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 = iTeTools.Cycle (ITER ({"Mirny resident", "Mafia"}))
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

To avoid (unlikely) file damage (for example, if the power suddenly disappears during the file recording), you can use a temporary file. See ThreadSafe and Fault-Tolerant File Writes .


Answer 2, AUTHORITY 60%

From the question, it is not very clear what you want to do. “Every time different” and “random” (ie, random) are completely different things. So let’s try to make two options.

option 1. Random string.

import random
Strings = ['String 1', 'String 2']
Print (Random.Choice (Strings))

option 2. Each time different string.

def next_string ():
  Strings = ('String 1', 'String 2')
  next_string.i = (next_string.i + 1)% Len (Strings)
  Return Strings [Next_String.i]
next_string.i = -1
Print ("" .join (next_string () for R in Range (10)))

And so you can implement the case when each time the next call, the program displays the following line:

def next_string (): 
Strings = ('String 1', 'String 2')
   If next_string.i & lt; 0:
     Try:
       With Open ("I.IDX", 'R') AS File:
         next_string.i = int (file.read ())
     Except Exception:
       Pass
   next_string.i = (next_string.i + 1)% Len (Strings)
   With Open ("I.IDX", 'W') AS File:
     File.Write (STR (next_string.i))
   Return Strings [Next_String.i]
next_string.i = -1
Print (next_string ())

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions