0
Good afternoon guys, I have the following code:
def Ler_vetor():
VET= []*10
for i in range(0,10):
VET.append(int(input('Digite um número: ')))
return VET
def Escreva_vetor(VET):
print('Dentro do vetor (permutado) tem:',VET)
def Somar_vetor(VET):
pares = []
for i in range (0,10):
if VET[i] % 2 == 0:
pares.append(VET[i])
soma = sum(pares)
print('A soma dos pares é: ',soma)
return pares
def Contar_vetor(VET):
contador = 0
for i in range (0,10):
if VET[i] % 2 == 0:
contador += 1
print('Possui dentro do vetor: {} pares'.format(contador))
return contador
def Trocar_vetor(VET):
for i in range(0,10,2):
aux = VET[i]
VET[i] = VET[i+1]
VET[i+1] = aux
print('----- Vetor A : -----')
vetA = Ler_vetor()
Somar_vetor(vetA)
Contar_vetor(vetA)
Trocar_vetor(vetA)
Escreva_vetor(vetA)
Basically it reads a vector of 10 positions, sums up all the pairs, counts how many pairs it has, switches the position odd by playing forward and writes.
this part for what I need is OK.
Now I would need to do the arithmetic mean of even numbers within the vector and I can not at all, and this mean cannot be through function, but in the main program, below the Type vector (Veta).
Could someone help me?
Here is an example of the output:
----- Vetor A : -----
Digite um número: 1
Digite um número: 2
Digite um número: 3
Digite um número: 4
Digite um número: 5
Digite um número: 6
Digite um número: 7
Digite um número: 8
Digite um número: 9
Digite um número: 10
A soma dos pares é: 30
Possui dentro do vetor: 5 pares
Dentro do vetor (permutado) tem: [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]
Thank you friend for the answer, but I am "obliged" to do the rest with functions: Read two vectors of integer numbers of 10 positions; Print the two vectors read; Calculate the sum of even numbers contained in each vector. Count how many even values there are in each vector For each vector read in the item to exchange odd-order elements with those of immediately following even-order for each vector Print the two vectors after the exchanges. Previous items should be developed by functions.No main program calculates the arithmetic mean of even numbers contained in vectors
– Gustavo Santos
@Gustavosantos: Feito!
– Lacobus
Thank you very much, however returned me this error:
Traceback (most recent call last):
Vetor A: [5]
Vetor B: [8]
 File "C:/Users/Dovah/PycharmProjects/trabalhoPROG/Trabalho2.py", line 56, in <module>
Soma dos Numeros Pares do Vetor A: 0
 media1 = sum(pares1) / float(len(pares1))
Soma dos Numeros Pares do Vetor B: 8
ZeroDivisionError: float division by zero
Qtd. de Numeros Pares no Vetor A: 0
Qtd. de Numeros Pares no Vetor B: 1
Vetor A Trocado: [5]
Vetor B Trocado: [8]
– Gustavo Santos
Note that the data entry is done in the format:
n1, n2, n3, ...
– Lacobus
My lack of attention, thank you so much I will study the code and try to redo!!
– Gustavo Santos