Learn practical skills, build real-world projects, and advance your career
Created 2 years ago
Control Structures
Question
A word is said to belong to the red team if it has the letter r in it.
Write an 'if' statement to check whether the given word belongs to the red team or not.
# write code here
s=input("Enter the word in small caps: ")
print("The word belongs to red team" if 'r' in s else "No")
Enter the word in small caps: real
The word belongs to red team
Question
In continuation to previous example, if the word has the letter 'b', it belongs to the blue team. And if the word has both 'r' and 'b' then first letter gets a precedence. And if the word does not have the letter 'r' or 'b', it does not belong to any team.
######Example: (word,output)
- rabbit = red team
- brand = blue team
- dog = no team
Write a condition for this decision.
# write code here
s=input("Enter word: ")
print("Red team" if s.find('r')<s.find('b') else "Blue team" if s.find('b')<s.find('r') else "No team")
# you might want to consider if statement inside a for loop
Enter word: brand
Blue team