Write a program to build a sentence based on a random number of words entered by
ID: 3907444 • Letter: W
Question
Write a program to build a sentence based on a random number of words entered by the user.
The random number of words is to be generated by the program and should be an integer between five and twelve.
The program should request a separate input for each word entered (only enter one word at a time).
Print the complete sentence in uppercase with an exclamation point at the end.
Use only python code, functions, methods and data structures that we have used in our course (this means not using/copying code you have encountered outside of assigned course materials).
Explanation / Answer
import random
import array
#generating a random number within the range 5 to 12
x=random.randint(5,12)
#print random number generated so that user can entered required number of words
print ("The random number of words to be entered : ",x)
i=0
#array to store the user input words
words=[]
#iterate i from 0-x to get user input
while(i<x):
#to show which number user has enter but not neccessary to print it
print("Input word %d : "%(i+1))
#store user input to words array
words.append(input())
#increment i to check condition
i=i+1
#initialize variable s to single space to separate 2 words
s=" "
#join array of words with space separation and convert complete string to UPPER case
upper=s.join( words ).upper()
s="!"
#adding exclamation point at the end.
output=upper+s
#print output
print (output)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.