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.