-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecret_word_replacement.py
More file actions
39 lines (34 loc) · 917 Bytes
/
Copy pathsecret_word_replacement.py
File metadata and controls
39 lines (34 loc) · 917 Bytes
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
# Secret Word Replacement
# Write a Python program that reads a sentence and replaces specific words according
# to the following rules:
# ● cat → dog
# ● apple → orange
# ● red → blue
# Words not listed above should remain unchanged.
# Input:
# cat likes red apple
# Output:
# dog likes blue orange
# sentence = input("Enter a sentence: ")
# words = sentence.split()
# for i in range(len(words)):
# if words[i] == "cat":
# words[i] = "dog"
# elif words[i] == "apple":
# words[i] = "orange"
# elif words[i] == "red":
# words[i] = "blue"
# print(" ".join(words))
#another method
sentence = input("Enter a sentence: ")
words=sentence.split()
replace={
"cat":"dog",
"apple":"orange",
"red":"blue"
}
for i in range(len(words)):
if words[i] in replace:
# print(words[i])
words[i]=replace[words[i]]
print(" ".join(words))