0
Hello! Please I need help with this:
Write a python program that asks the user to enter a word and then capitalizes every other letter of that word. So if the user enters rhinoceros, the program should print rHiNoCeRoS.
3 Réponses
+ 3
# look at the task. You need to find a way to capitalise the every second word. You can use "for loop" to iterate and find the second using "if statement" and capitalize that and use "a variable" to store the output
word = input()
output = ""
for i in range(len(word)):
if i % 2 != 0:
output += word[i].upper()
else:
output += word[i]
print(output)
# Here if you look at the 'if statement', i am checking for if 'i variable' is not divisible by 2 because index starts with 0 in python.
+ 2
You can try splitting the input into a list, then capitalize required letters using a loop, and finally join the list.
0
Exactly, this sort of problem is usually solved using conditional logic inside a loop and evaluating if the iterator variable is even or odd, which makes for one of the top uses of the modulo (%) operator across programming languages.
Also, you'll find this pattern of evaluating if the current iteration "turn" is even or odd useful to make turn-based game logic.