How to make a text generator according to the possibilities I put in it

0

I've been looking for a few days about how to combine a random text according to the possibilities that I put inside the parentheses. Example: (Hello, Hi, Good afternoon) How are you? (Your day is fine? How long !!!) My question is: how do I generate a different text with one of the possibilities that it has inside the parentheses, what is outside is to remain the same. Someone gives me a lightzzzzzzzzzzz, so trying on python

    
asked by anonymous 26.12.2018 / 11:54

2 answers

2

Python has a native function for this: random.choice .

from random import choice

saudacoes = ('Olá', 'Oi', 'Boa tarde')
saudacao = choice(saudacoes)

print(f'{saudacao}, como vai você?')

In this way, every time the code is executed, a string of saudacoes will be chosen randomly.

    
26.12.2018 / 12:17
-1

Put the options inside a tuple, shuffle with the random.shuffle method, and take the first position.

from random import shuffle

op1 = ['Olá', 'Oi', 'Boa tarde']
shuffle(op1)

op2 = ['o seu dia está ótimo?', 'a quanto tempo!!!']
shuffle(op2)

print "{} como vai você {}".format(op1[0], op2[0])

In this way, the phrases will automatically change

    
26.12.2018 / 12:27