-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp39.py
More file actions
43 lines (32 loc) · 1.24 KB
/
Copy pathp39.py
File metadata and controls
43 lines (32 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# p39.py
# Rishi Saravanan
# Python 3.11.9
# Description:
'''
Write a program that asks the user to enter a sentence.
Your program will:
Show how many words are in the sentence
Show the last word of the sentence
Ask the user to enter their own word, and count how many times their word appears in the sentence
Note: you can't use the built-in python function count() to do this!
'''
sentence = input("Please enter a sentence: ")
print()
sentenceSplit = sentence.split(" ")
last = sentenceSplit[len(sentenceSplit)-1]
print("There are", len(sentenceSplit), "words in the sentence you entered")
print("The last element of the list is", last)
word = input("Please enter a word to search: ")
count = 0
for i in range(0,len(sentenceSplit),1):
if sentenceSplit[i] == word:
count += 1
print("The word '" + word + "' appears " + str(count) + " times in the sentence.")
'''
======================= RESTART: C:\Users\rishi\python\p39.py =======================
Please enter a sentence: i like to play violin and i like basketball
There are 9 words in the sentence you entered
The last element of the list is basketball
Please enter a word to search: like
The word 'like' appears 2 times in the sentence.
'''