-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathif_elif_else.py
More file actions
31 lines (27 loc) · 1.15 KB
/
if_elif_else.py
File metadata and controls
31 lines (27 loc) · 1.15 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
#Raw code
amino_acids = ["Cystein", "Cystein Cystein", "Cystein Cystein Cystein"]
for aa in amino_acids:
#If Cystein is not found between character 20 and 40 inclusive, print Cystein not found
if aa.find("Cystein", 0, 40) == -1:
print("Cystein not found")
#Count occurrences and replace two with one
elif aa.count("Cystein") == 1:
print(aa)
elif aa.count("Cystein") == 2:
print(aa.replace("Cystein Cystein", "Cystein"))
else:
print(aa.replace("Cystein Cystein Cystein", "Cystein")) #Replace three occurrences with one
#aa.replace(intended_value_for_replacement, new_value): if the intended value for replacement is not found in aa, return the original value in aa.
#aa = "Cystein Cystein"
#aa.count("Cystein")
#2
#More efficient code
amino_acids = ["Cystein", "Cystein Cystein", "Cystein Cystein Cystein"]
for aa in amino_acids:
#If "Cystein" is not found in the first 40 characters
if "Cystein" not in aa[:40]:
print("Cystein not found")
else:
#Replace multiple occurrences of "Cystein" with a single "Cystein"
res = "Cystein" if aa.count("Cystein") > 1 else aa
print(res)