Higher and lower value problem with while. (No list!)

Asked

Viewed 11,856 times

0

Write a program that reads an integer N and then read N real numbers, separating the smallest and largest, presenting them on the screen.

N = int(input("Digite N: "))
i = 0
ma = 'maior'
me = 'menor'
me = x
while i < N:
    x = float(input("Digite um número: "))
    ma = x
    me < ma
    i = i + 1
    if x > ma:
        ma = x
    if x < me:
        me = x
print ('O maior valor digitado foi {} e o menor foi {}'.format(ma,me))

In the program, the user is asked to enter an integer value N and then read N real numbers, thus showing the lowest and highest value among those. The point is that I am doing it without using the list method. (beginner programming.) This code works in some tests, but not in others. I know the problem is in the declaration of variables or in the conditions of the functions if. But I do not see/ I can fix these errors.

4 answers

5

Your code:

  1 N = int(input("Digite N: "))                                                                                                                                                                                                 
  2 i = 0                                                                                                                                                                                                                        
  3 ma = 'maior'                                                                                                                                                                                                                 
  4 me = 'menor'                                                                                                                                                                                                                 
  5 me = x                                                                                                                                                                                                                       
  6 while i < N:                                                                                                                                                                                                                 
  7     x = float(input("Digite um número: "))                                                                                                                                                                                   
  8     ma = x                                                                                                                                                                                                                   
  9     me < ma                                                                                                                                                                                                                  
 10     i = i + 1                                                                                                                                                                                                                
 11     if x > ma:                                                                                                                                                                                                               
 12         ma = x                                                                                                                                                                                                               
 13     if x < me:                                                                                                                                                                                                               
 14         me = x                                                                                                                                                                                                               
 15 print ('O maior valor digitado foi {} e o menor foi {}'.format(ma,me))

Considerations:

  • In lines 3 and 4 you define the variables ma and me as strings, which makes no sense;

  • Already on line 5 you put that me will equal to x, where the object x is not even defined yet;

  • On line 8, you define that whenever a new number is read, the higher value, ma, will be equal to it; why update the value of ma at this point?

  • On line 9 there is a random expression that simply made no sense;

  • In line 11 you compare if the read value is greater than ma, but as on line 8 put that ma = x, they will always be equal and therefore the condition will never be satisfied;

Other lines that I did not comment even make sense, such as reading the value of N, the loop of repetition while, the reading of x and updating the lowest value.

0

This implementation resolves:

n = int(input("Digite N: "))
ma = None
me = None
for i in range(n):
   x = float(input("Digite um número: "))
   ma = ma if ma != None and ma >  x else x
   me = me if me != None and me < x else x

print ('O maior valor digitado foi {} e o menor foi {}'.format(ma,me))
  • 1

    Please avoid answering only with codes. What may be trivial to you can be complex to other users, so always look to detail the solution as best as possible, including describing it in text.

  • 1

    And your solution has a small flaw, because in this case, you cannot make the condition if ma or if me only, since any value that is assessed as false, such as 0.0, does not satisfy it. Consequence of this, for example, if you enter two numbers: [0.0, -1.0], the program will rate -1.0 as the largest (link).

  • You’re right... I got it right.

-1

x = 'S'
cont = 0
maior = 0
menor = 0

while x != 'N':
    z = int(input('Digite um numero: '))
    cont += 1
    
    if cont == 1:
       maior = z
       menor = z
    else:
        if z > maior:
            maior = z
        if z < menor:
            menor = z

    x = str(input('Deseja continuar? S - Sim / N - Não  ')).upper()

print(f'O MAIOR número digitado foi {maior}.')
print(f'O MENOR número digitado foi {menor}.')

-2

num = int(input('Digite um numero: '))
maior = num
menor = num

cc = str(input('Deseja continuar? S - Sim / N - Não  ')).upper()

while cc not in 'N':
    num = int(input('Digite um numero: '))
    
    if num > maior:
        maior = num
    if maior < menor:
        menor = num

    cc = str(input('Deseja continuar? S - Sim / N - Não  ')).upper()


print('O MAIOR número digitado foi {}.'.format(maior))
print('O MENOR número digitado foi {}.'.format(menor))

Browser other questions tagged

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