My teachers has set me the task:
Write a sub-routine that
reads in the cities from text file cities.txt (which just contains the names of 7 cities within),
adds them to an array,
reverses the city names without using the inbuilt .reverse option in Python. ie. "london" would become "nodnol"
I have added them to an array however have not been able to reverse each letter then append the reversed string back into the array. right now the output is just the names of the 7 cities within the array whereas I want for each character in each name to be reversed my code is:
cities = []
def read_and_reverse():
reversedCities = []
f = open("cities.txt", "r")
for i in f:
i = i.strip("\n")
cities.append(i)
print(cities)
for city in cities:
for i in range(0,len(city)):
index = len(int(i))
while index:
index -= 1
reversedCities.append(city[index])
return ''.join(reversedCities)
print(reversedCities)
read_and_reverse()