How to get an item in a list based on a number in Python?

Question:

I am trying to code something in Python that makes the user type random phrases. If you manage to find out how to do this, I would appreciate it.

import pyautogui
import random

phrases = ["Phrase1", "Phrase2", "Phrase3"]

while True:
    pyautogui.write(list.index(phrases, random.randint(1, len(phrases))))

I was expecting to run the code and be able to type a random phrase (e.g. running the code and either typing "Phrase1", "Phrase2", "Phrase3")

Asked By: 0mgRod

||

Answers:

The code should be changed to:

import pyautogui
import random

phrases = ["Phrase1", "Phrase2", "Phrase3"]

while True:
    phrase = random.choice(phrases)
    pyautogui.write(phrase)
Answered By: Mohamed Saeed
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.