Question

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

  1. 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 to record the time it takes the user to correctly copy the sentence every time, and output the results to a file called “times.txt”,

with the each sentence and the time to complete each and finally calculate the average time. The format should look similar to this:

Sentence                        Time to complete             

“….”                                 “time”                                   

“….”                                 “time”                                   

“….”                                 “time”                                   

“….”                                 “time”                                   

“….”                                 “time”                       

Average: average time

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

Here is the answer for your question in PYTHON programming language.

CODE:

#importing required modules
import random
import time

#creating two lists to store sentences and time_taken for each
sentences = []
time_took = []
#random_sentence method that returns a randomly generated senence
def random_sentence():
####################################################
#Words to create a sentnece
articles = ("The","A","The","A")
nouns = ("dog","cat","mouse","rabbit")
verb = ("is","was","was","is")
adv = ("fastly","slowly","hungrily","fastly")
verbs = ("running","walking","searching","jumping")
prep = ("towards","on","for","on")
inter = ("corridor","wall","cheese","bushes")
######################################################
#Number generated randomly between 0 and 4
#I took the random range as 0 to 4 because there are four possible words in each parts of speech
num = random.randrange(0,4)
#As you asked for sentence with <=7 words I choose 7 words as my choice
#You can choose whatever the length of sentence you want
#Choosing word at random number index from all parts of spech and storing it in the sentence
sentence = articles[num] + " " + nouns[num] + " " + verb[num] +
" " + adv[num] + " " + verbs[num] + " " +prep[num]+ " " + inter[num]
#returing the generated sentece
return sentence
#j variable initialised to 0 for loop requirement
j = 0
#write_results function that takes two lists sentences and time_took lists as arguments
def write_results(list_sentence , time_taken):
#Statements to be printed for unserstanding
print("\n---------\t\t\t\t\t\t\t ----------------")
print("Sentnece \t\t\t\t\t\t\t Time to complete")
print("---------\t\t\t\t\t\t\t ----------------")
#Initially the average and time_taken are set to 0.00
average = 0.00
total_time = 0.00
#loop to repeat for 5 times
for j in range(5):
#Printing jth element of list_sentence and jth element of time_take converting it to string
print(list_sentence[j] + "\t\t\t\t\t\t\t " + str(time_taken[j]))
#Adding time_taken to total_time at each iteration
total_time = total_time +time_taken[j];
#Finally the average is total_time by 5
average = total_time/5;
#Rounding avaerage to 2 decimal points
average = round(average ,2)
#Printing average time taken
print("\nAverage time taken is :"+str(average))

#Creating variable i an initialising it to 1
i = 1
#loop that repeats for 5 times
while i <=5:
#Each iteration calls random_sentence() and stores the resulted sentence in sentence variable
sentence = random_sentence()
#Printing the sentence
print(sentence)
#Calculating start time from which user starts typing
start = time.time()
#Prompting user for input
enter_sent = input("Rewrite the sentence\n")
#Calculating end time when user completes
end = time.time()
#calculating time _taken
time_taken = end - start
#rounding it to 2 decimals
time_taken = round(time_taken,2)
#loop that repeats until the user types the same sentence
while 1:
#If same sentence is typed the sentence is added to sentences list
#and time taken is added to time_took list
#printing the time_taken and exiting the while loop
if sentence == enter_sent:
sentences.append(sentence)
print("Time taken" +str(time_taken))
time_took.append(time_taken)
break
#Else asking the user for retype the sentence again
else:
print("Typed Wrong \n")
#calculating starting and ending time
start = time.time()
enter_sent = input("Rewrite the sentence\n")
end = time.time()
time_taken = end - start
#incrementing i value for each iteration
i = i+1

#calling wrte_results function finally
write_results(sentences,time_took)

SCREENSHOTS

Please see the screenshots below for indentations of the code.

OUTPUT

When sentences are typed right

When sentences typed wrong

NOTE : Please check the indentations of the code while you copy it and also check if any unnecessary dots added in a black background. Please remove comments while you run the script for smooth output.

Add a comment
Know the answer?
Add Answer to:
Write a python3 function random_setence to implement a sentence comprised of length <=7 randomly generated 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
  • Write a program that calculates the average of a stream of non-negative numbers. The user can...

    Write a program that calculates the average of a stream of non-negative numbers. The user can enter as many non-negative numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, zero counts as a number that goes into the average. Of course, the negative number should not be part of the average (and, for this program, the average of 0 numbers is 0). You must use a method to read...

  • ***** PSEUDOCODE PLEASE****** ***** PSUDOCODE PLEASE****** Program 0 (Warm-up): Deoxyribonucleic acid, or DNA, is comprised of...

    ***** PSEUDOCODE PLEASE****** ***** PSUDOCODE PLEASE****** Program 0 (Warm-up): Deoxyribonucleic acid, or DNA, is comprised of four bases: (G)uanine, (C)ytosine, (A)denine and (T)hymine.  Ribonucleic acid, or RNA, is different than DNA in that it contains no Thymine; thymine is replaced with something called (U)racil.  For this assignment, you will create an array of 255 characters.  You must start by filling the array with random characters of G, C, A and T.   You must then print out the array.  Next, replace all the instances of Thymine...

  • Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( );...

    Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...

  • This part requires you to write two programs, compile them, and execute them - just like...

    This part requires you to write two programs, compile them, and execute them - just like our lab questions.  You may access these questions as many times as you need to and you have the entire duration of the test to complete these problems. You must submit the program code and the sample outputs for each problem - just like lab. These questions are worth 6 points each. 1.      C++ Write a program that reads 20 data values from a data...

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • You should implement several functions that the Phone Contacts program will need Each function with take...

    You should implement several functions that the Phone Contacts program will need Each function with take in data through the parameters (i.e., variables names listed in parentheses) and make updates to data structure that holds the contact information, return, or print information for a particular contact. The parameters needed are listed in the table below and each function is described below that 1. Make an empty dictionary and call it 'contacts'. Where in your code would you implement this? Think...

  • Exercise 2 Write a program that simulates the Texas Powerball lottery. You will have to do resear...

    I HAVE A PROBLE WITH THIS JAVA PROBLEM CAN ANYONE HELP ME PLEASE. Exercise 2 Write a program that simulates the Texas Powerball lottery. You will have to do research to find out how many numbers are drawn, and what the range(s) of the numbers are. For this exercise you need to create a class called "Powerbali", which exposes a public function called play'. This function accepts several parameters , which represent the individual numbers the player wants to play....

  • PYTHON PROGRAMMING NEED HELP ASAP You will write an application to manage music collections -- a music collection is a l...

    PYTHON PROGRAMMING NEED HELP ASAP You will write an application to manage music collections -- a music collection is a list of albums. The named tuples used for Albums and Songs are defined below, and an example of a music collection is given. (DO NOT HARDCODE THE VALUES FOR MUSIC!) Album = namedtuple('Album', 'id artist title year songs') Song = namedtuple('Song', 'track title length play_count') MUSIC = [ Album("1", "Peter Gabriel", "Up", 2002, [Song(1, "Darkness", 411, 5), Song(2, "Growing Up",...

  • WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length,...

    WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length, and depth. It will return the total surface area (6 sides) of the rectangular box it represents. 2. Implement a method named rightTriangle that accepts 2 double arameters named sideA and hypotenuseB. The method will return the length of the third side. NOTE To test, you should put all of these methods into a ‘MyMethods’ class and then write an application that will instantiate...

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
ADVERTISEMENT