You can create a function in your code to find all cities that have a rate within a certain range set by you or the user.
Within this function just go through the cities and check if the rate of the city in question is within the range set by the user with the conditional: start <= taxa <= stop
.
To do this check we will need to get the city rate on type int
, separating it from the city name using the method split
and removing the line break at the end of the string.
# A variável cidades é um iterável com todas as linhas do arquivo.
for cidade in cidades:
taxa = int(cidade.split(" = ")[-1].replace("\n", ""))
if start <= taxa <= stop: cidades_encontradas.append(cidade)
For the function to become more dynamic, we can define None
as default value for the range stop point. If the user does not pass a value for the parameter stop
, this variable will later receive the value passed to the parameter start
.
def encontrar_cidades(cidades, start, stop = None):
cidades_encontradas = []
stop = start if not stop else stop
This means that the user is not required to declare a range, and may search cities that have a specific fee. See the full function code below:
def encontrar_cidades(cidades, start, stop = None):
cidades_encontradas = []
stop = start if not stop else stop
for cidade in cidades:
taxa = int(cidade.split(" = ")[-1].replace("\n", ""))
if start <= taxa <= stop: cidades_encontradas.append(cidade)
return cidades_encontradas
Now that we’ve created the function responsible for finding cities based on the isolation rate, we’ll need to apply it to the program.
To do this, just create some conditionals for each type of input
typed by the user, setting the arguments of start
and stop
based on user input:
taxa = input("Digite um intervalo de taxa de isolamento:")
if "<50%" == taxa: start, stop = 0, 50 # <50% | (0, 50)
elif ">60%" == taxa: start, stop = 60, 100 # >60% | (60, 100)
elif "50% <= x <= 60%" in taxa: start, stop = 50, 60 # 50% <= x <= 60% | (50, 60)
else: start, stop = int(taxa), None # Taxa X | (x, x)
cidades_encontradas = encontrar_cidades(conteudo, start, stop)
print("\nCidades encontradas:", cidades_encontradas)
Since the function we create gives us the freedom to use the range we want, you can give the user more flexibility to search cities with the range he wants.
See the example below to understand what I mean:
if "<" == taxa[0]:
taxa = int(taxa[1:].replace("%", ""))
start, stop = 0, taxa
In this example, if the user type "<"
before the rate, means that the user wants cities with the rate less than or equal to X. Thus, we do not need to limit ourselves to search for rates "<50%"
.
Remember that it is important to always close the file after you have made its use. To close the file, you can use the method close
or the with
to close the file automatically.
# Utilizando o método "close" para fechar o arquivo.
arquivo = open('arquivo.txt')
conteudo = arquivo.readlines()
arquivo.close()
# Utilizando a declaração "with" para fechar o arquivo.
with open('arquivo.txt') as arquivo:
conteudo = arquivo.readlines()