0
votes

I know there is a min max syntax but I just wanted to do it myself using if but the code i wrote keep returning 0 0 where is my mistake at? The program is supposed to print out larges and least number from a space separated numbers input

user = input(" :) ")

user = user.split(' ')

lt = 0
gt = 0
gtn = 0
ltn = 0

for i in user:
    for j in range(0, len(user)):
        if int(i) < int(user[j]):
            lt += 1
        elif int(i) > int(user[j]):
            gt += 1
        else:
            pass
    if lt == len(user):
        ltn = i
    elif gt == len(user):
        gtn = i

print(gtn, ltn)
1
plz add some sample input outputSM Abu Taher Asif
What is it supposed to do?khelwood
lt will never be equal to len(user), as if int(i) < int(user[j]) will always fail when user(j) is iThierry Lathuille

1 Answers

0
votes

You don't need a nested for, neither so many if conditions. All you have to do is initialize min to a very large value and max to a very small value then iterate once.

nums = map(int, input(" :) ").split())
min_n = float('inf')
max_n = -float('inf')

for n in nums:
    if n < min_n:
        min_n = n
    if n > max_n:
        max_n = n

print(min_n)
print(max_n)

Sample output:

:) 3 4 5 6 2 11 2 3 4 99 1 -1
-1
99