Avatar ·

Random text in Python

📁 Advanced Python Topics, example, Beginner

How to generate random text in Python? So that it outputs only one variable out of two? For example, I have two variables with these values:

import randomrole1 = "Civilian"role2 = "Mafia"role = random.randint(role1, role2)print("You are - " + role)input("")

And I need that when outputting the text, ONLY ONE VARIABLE is displayed. And each time it should be different - sometimes role1, sometimes role2, then role1 again.

I hope I explained it clearly. I'm a beginner in Python and I ask you to answer in more detail.

Avatar ·

Unfortunately, Google Colab doesn't have the math.isqrt function because it uses Python 3.7.11. As for the other functions, you can check the speed like this (a small loop is added to prevent caching, otherwise without the loop %timeit complains that the results might have been cached, although they have roughly the same ratio, I checked, but it's better to take precautions):

import mathn = 100%timeit [x ** 0.5 for x in range(n)]%timeit [pow(x, 0.5) for x in range(n)]                                

Log in to leave an answer

Blogs