I can not print a variable that receives the value of a list position

0

List exercise.

Select the largest and smallest element of a vector.

import os
os.system("clear")
vetor=[] #declara o vetor
#solicita o n° de elementos
n = int(input("Digite a quantidade de elementos a ser adicionada.."))
#declara o i
i = 0
#recebe os elementos do vetor (numeros inteiros)
while (i<n):    
    temp = int(input("Digite o elemento a ser adicionado.."))
    vetor.append(temp)#vetor na última posição recebe o valor salvo em temp
    i=i+1
print(vetor)
#Reiniciando o i
i=0
#declara variáveis menor e maior
menor = vetor[0]
maior = vetor[0]
#Laço de seleção do menor e do maior valor
while (i<n):    
    if vetor[i]<menor:
        menor=vetor[i]
    if vetor[i]>maior:
        maior=vetor[i]
        i=i+1
#Imprime o resultado
print("Vetor dado: ", vetor)
print("Menor valor: ", menor)
print("Maior valor: ", maior)
    
asked by anonymous 07.09.2015 / 00:29

2 answers

1

The logic of your program is correct - you apparently were only confused about the nature of the blocks in Python - all the lines indented forward, after a command that ends in : are part of a block that will be executed or repeated together, depending on that command. In the case of if all indented forward lines will only be executed if the condition is true. Most other languages in use today have inherited the C syntax and use keys to delimit these blocks - ( { and } ).

In case of your listing, notice that you are only incrementing the value of i if the second condition is true:

while (i<n): 
    ...
    if vetor[i]>maior:
        maior=vetor[i]
        i=i+1

And so, the value of i never equals n and your program gets stuck indefinitely in the while loop. Leave the line i = i + 1 alinahda under if , and it will be exercised for every element in the list, and your program will run normally.

That said, note that while this program is cool compared to an equivalent program in C or Pascal, Python is a language that makes the basics of programming really basic. Well, besides the things we use "seriously" in everyday life, which are the built-in functions min and max that answer the highest and lowest value at a single time, as Orion's answer points out, there are some tips for you to check out there, without needing to short circuit your whole program:

In Python, the for loop always traverses all elements of a sequence - does not need the numeric value i as the sequence index. Since your numbers are in the vetor list, just do:

for elemento in vetor:
    if elemento > maior:
        ...
    if elemento < menor:
        ...

Note that in this way you do not rpecise the variable plus i , nor write vetor[i] - you will already retrieve each elento of vetor in variable elemento . It would have some more tips - but the best thing right now is you practice and go discovering things.

    
08.09.2015 / 05:15
1

You can return the lowest and highest value using min() and max() respectively.

Example:

vetor = []

quantidade = int(input('Digite a quantidade de elementos: '))

for i in range(quantidade):
    elemento = int(input('Digite o elemento: '))
    vetor.append(elemento)

print('Vetor: ', vetor)
print('Menor valor: ', min(vetor))
print('Maior valor: ', max(vetor))

With fewer rows:

quantidade = int(input('Digite a quantidade de elementos: '))
vetor = [int(input('Digite o elemento: ')) for i in range(quantidade)]
print('Vetor: ', vetor)
print('Menor valor: ', min(vetor))
print('Maior valor: ', max(vetor))
    
07.09.2015 / 01:56