-1

I'm trying to automate the telegram messaging platform and to send a reply, script must auto select a random reply from the array I provided. Though the script is selecting randomly from the array it keep on sending the same reply. All I would like to see is a different selection each time.

I have already tried looking at How to randomly select an item from a list? and it seems this doesn't fit my need.

#content of the automatic reply
msg = ['hello','Hmmmm','what','alright','Done']
message = random.choice(msg)

[img] https://i.stack.imgur.com/dSA5G.jpg [/img]

When the user sends a message script must return a message from the provided array each at a time in a random manner. I am newbie to python and I appreciate any assistance here.

GKE
  • 960
  • 8
  • 21
Maverick
  • 59
  • 2
  • 2
  • 13

1 Answers1

0

How many times did you tried? It should work:

import random

i=0
while i < 20:
    message = random.choice(msg)
    print(message)
    i+=1

output:

alright
what
what
alright
Done
what
hello
what
alright
alright
Hmmmm
Hmmmm
Hmmmm
hello
Hmmmm
Hmmmm
hello
hello
Hmmmm
hello

If otherwise what you want to is to have a different choice each time, you could try to remember last choice and avoid using it:

import random

msg = ['hello','Hmmmm','what','alright','Done']
message = random.choice(msg)
last_choice = message
i = 0
while i < 20:
  new_list = [ m for m in msg if m != last_choice ]
  message = random.choice(new_list)
  print(message)
  last_choice = message
  i+=1
cccnrc
  • 1,195
  • 11
  • 27