I cannot print a variable that receives the value of a list position

Asked

Viewed 552 times

0

Listing 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)
  • 1

    I’m voting to close this question as out of scope because we don’t do homework for each other.

  • Really. I agree with @Omni. It seems to me a bad use of the site and a bad way to learn to program.

  • 1

    Although the question is about homework, it is in the scope yes: the author fez the work and has an error in its logic that is almost corrteta- just asks for help to find the error. It is very different from someone who only puts the statement and expects others to write the whole program, and is completely within the scope and spirit of S.O.

  • @Alex: When you post some questions, in addition to the listing, also put what is going wrong. In this case it would be "the program gets stuck doing nothing". When you have an error, the error message is important. Write about what you did, and what you think might be the mistake - don’t just put the statement of the question.

2 answers

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 lines:

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))
  • 1

    Thank you so much for your help! I managed to solve the problem by other means and forgot that I had published it here. Thank you!

1


The logic of your program is correct - you have apparently 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 lines forward will only be executed if the condition is true. Most of the other languages in use today inherited the syntax of the language C and uses keys to delimit these blocks - ({ and }).

In the case of your listing, note that you are only increasing 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 looks like n and your program gets stuck indefinitely in the loop while. Leave the line i = i + 1 aligned under the if, and it will be exercised for every element of the list, and your program will run normally.

That said, note that while this program is cool when 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 minand max that respond the highest and lowest value at a single time, as well as points out Orion’s response, there are some tips for you to check there, without needing to short-circuit your entire program:

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

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

Note that this way you do not need the most variable i, or write vetor[i] - or to retrieve each element of the vetor in the variable elemento. I would have some more tips - but the best thing right now is for you to practice and figure things out.

  • Thank you very much for the help, very complete answer! I managed to solve the problem by other means and forgot that I had published it here. Thanks again!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.