r/pythonhelp • u/Lethal_candle • Jun 16 '20
Hi guys I'm a beginner in python, and would like help writing the following program
Write a Python program that prompts the user to enter a sentence of at least 5 words. Include a test to ensure that the user has entered at least 5 words. Store the individual words as separate items in a list. Print the sentence in reverse order - word by word. The words must print in one line (not beneath each other).
4
Upvotes
1
u/sentles Jun 17 '20
while True:
inp = input("Enter a sentence of at least 5 words: ")
inp = inp.split()
if len(inp) < 5:
print("Expected at least 5 words.")
else:
break
inp.reverse()
#for python 2
for word in inp:
print word,
#for python 3
print(*inp, sep = " ")
3
u/robohobo- Jun 16 '20
To take input use
str = input("Enter something")You could try
str.split(" ")to split it into list of words.You can use
len(list)to check length of list.You can use
reverse(list)to reverse a list.You can use
" ",join(list)to change a list to a string.