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

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 and thus show the lowest and highest value among them. The question is what am I doing without using the list method. (beginner programming.) This code works on some tests, but on others it does not. I know the problem is in the declaration of variables or in the conditions of the if functions. But I can not see these errors.

    
asked by anonymous 29.03.2018 / 16:19

2 answers

2

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 line 8, you define that whenever a new number is read, the largest value, ma , will be equal to it; why update the me value at this point?

  • On line 9 there is a random expression that simply did not make any sense;

  • On line 11 you compare if the read value is greater than me , but as in line 8 you put x , they will always be equal and therefore the condition will never be satisfied; p>

Other lines that I have not commented on make sense, such as reading x , repeat loop ma , reading ma , and updating the lowest value.

    
29.03.2018 / 17:01
-1

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))
    
29.03.2018 / 17:33