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 min
and 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.
I’m voting to close this question as out of scope because we don’t do homework for each other.
– Omni
Really. I agree with @Omni. It seems to me a bad use of the site and a bad way to learn to program.
– sergiopereira
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.
– jsbueno
@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.
– jsbueno