Question

in python: Write a function, write_file, that expects as arguments, in order: A list of words,...

in python:

Write a function, write_file, that expects as arguments, in order:

  1. A list of words, like the one created by the previous function
  2. A path to an output file
  3. The number of words to write to the output file
  4. The seed for the random number generator

and writes a randomly-generated file of that many words by randomly selecting words from the list.

If you use the random module function random.choice() to select each word from the list, calling the function like this:

>>> write_file(d, "./sample_output.txt", 20, 0)


should create a file called sample_output.txt in the current working directory, containing:

step confusing army turns fighting for hes secret up love waving favorite do have do defeat yo we weapon defeat

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The Code below will help you generate the required output:

Main.py

import random
d = ["confusing","army","turns","fighting","for","hes","secret","up","love","waving","favorite","do","have","do","defeat","yo","we","weapon","defeat"]
path= "Output.txt"

#function definition
def write_file(lis,path,num1,seed):
file1 = open(path,"a+")
random.seed(seed)
for i in range(0,num1):
index = random.randint(0, len(lis)-1)
file1.write(lis[index] + " ")

#calling Function
write_file(d,path,20,0)

Screenshot of code

X Lè main.py - C:/Python37/main.py (3.7.3) - 0 File Edit Format Run Options Window Help import random d = [confusing, army

Output

Output is Obtained in Output.txt File on RHS of picture below0 x lè Output.txt - C:\Python37/Output.txt (3.7.3) - File Edit Format Run Options Window Help have do army love we yo have wa

Add a comment
Know the answer?
Add Answer to:
in python: Write a function, write_file, that expects as arguments, in order: A list of words,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3.6 Question 12 (2θ points) write a function named uniqueWords that counts how many different...

    Python 3.6 Question 12 (2θ points) write a function named uniqueWords that counts how many different words there are in each line of an input file and writes that count to a corresponding line of an output file. The input file already exists when uniqueWords is called. uniquewords creates the output file. Input. The function uniquewords takes two parameters: ◆ inFile, a string that is the name of text file that is to be read. The file that inFile refers...

  • PYTHON CODE In a program, write a function that accepts two arguments: a list, and a...

    PYTHON CODE In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. Output should look like: Number: 5 List of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List of numbers that are larger than 5: [6, 7, 8, 9, 10]

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • In python write a module named randStr.py that has 3 functions. Each function has an optional...

    In python write a module named randStr.py that has 3 functions. Each function has an optional second parameter to set a particular seed. 1) randWord accepts a string and will return a random word from that string (return value is string). 2) strMixer will randomly change words inside the string and returns a string -- If the string is only one word, then it will mix the letters of the word and return the mixed word as a string 3)...

  • python In a program, write a function that accepts two arguments: a list, and a number...

    python In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display the number n, the list of numbers, and a sub list of all the numbers in the list that are greater than the number n. Initialize the list and the number n in the main function of your code and call your function, passing the list and number as arguments. Run your code...

  • In Python 4. outputWordPointPairs(pointWordList, filename, toFile) NO return (just prints a formatted list or writes it...

    In Python 4. outputWordPointPairs(pointWordList, filename, toFile) NO return (just prints a formatted list or writes it to file). NOTE: Your function should add the .txt extension to the filename before opening a file with that name. Write a function which will output the (pointValue, word) pairs in pointWordList to the shell or to a file depending on the bool value toFile. Note the order of elements of the tuple is (pointValue, word) not (word, pointValue). Find out why this specific...

  • Write a function called most_consonants(words) that takes a list of strings called words and returns the...

    Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...

  • PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """...

    PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as input. The output is the sentence after censoring all the matching words in the sentence. Censoring is done by replacing each character in the censored word with a * sign. For example, the sentence "hello yes no", if we censor the word yes, will become "hello *** no" Note: do not censor the...

  • In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq)....

    In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter words to the function...

  • Write a python3 function random_setence to implement a sentence comprised of length <=7 randomly generated words....

    Write a python3 function random_setence to implement a sentence comprised of length <=7 randomly generated words. Have this be implemented 5 times. Then let the user try and type out the sentence as fast as they can. After each successful copy of the sentence by the user, have the program sleep for a few seconds and generate a new random sentence while the user waits. Then repeat the previous 2 steps for 5 times. Finally write a function called write_results...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT