Mohamed Elsersy - 1 year ago 179

Python Question

when I enter a sequence of numbers like [2, 6, 9, 4, 8 ,7] to find the largest and smallest number it shows that (8, 7) are the largest and smallest numbers what is the error in my code?

`def minmax():`

x = int(input("Enter the number of integers you want: "))

mylist = [int(z) for z in input("Enter the numbers separated by a space and then click Enter: ").split()]

l = mylist[0]

for i in range(1, x):

if mylist[i] > mylist[i - 1]:

l = mylist[i]

else:

a = i

while a < x - 1:

if mylist[a + 1] > l:

l = mylist[a + 1]

a += 1

s = mylist[0]

for i in range(1, x):

if mylist[i] < mylist[i - 1]:

s = mylist[i]

else:

a = i

while a < x - 1:

if mylist[a + 1] < s:

s = mylist[a + 1]

a += 1

print((l, s), "are the largest and smallest numbers")

minmax()

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

If u only want to find the largest and the smallest number, you can let out the *else* statements. This works:

```
x = int(input("Enter the number of integers you want: "))
mylist = [int(z) for z in input("Enter the numbers separated by a space and then click Enter: ").split()]
l = mylist[0]
for i in range(1, x):
if mylist[i] > l: # I changed mylist[i-1] to l
l = mylist[i]
s = mylist[0]
for i in range(1, x):
if mylist[i] < s: # I changed mylist[i-1] to s
s = mylist[i]
print((l, s), "are the largest and smallest numbers")
minmax()
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**