gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Write a program with Python that takes 24 words from the input and arranges them…
- User
Write a program with Python that takes 24 words from the input and arranges them randomly and stores them in a text file.
- Assistant
You can create a Python program to achieve this by using the
randommodule to shuffle the list of words. Here's a step-by-step guide and the corresponding code:- Input the words: You need to accept 24 words from the user.
- Shuffle the words: Use the
random.shuffle()method to randomly arrange the words. - Write to a file: Save the shuffled words into a text file.
Here's the code implementing the above steps:
import random def main(): # 1. Input 24 words from the user print("Please enter 24 words:") words = [] for i in range(24): word = input(f"Enter word {i + 1}: ") words.append(word) # 2. Shuffle the words random.shuffle(words) # 3. Write the shuffled words to a text file filename = "shuffled_words.txt" with open(filename, "w") as file: for word in words: file.write(word + "\n") print(f"The words have been shuffled and saved to {filename}") if __name__ == "__main__": main()