Reputation: 17
It is a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered it prints out the largest and smallest of the numbers.
Custom Inputs are given as follow:
7
2
11
flip
10
4
done
Output:
Invalid Input
Maximum is 10
Minimum is 4
Expected Output:
Invalid Input
Maximum is 11
Minimum is 2
a=list()
while True:
sval=input()
if sval == "done":
break
try:
value=int(sval)
a.append(value)
except:
print("Invlid")
continue
def sml():
val=a[0]
for i in range(len(a)):
if a[i]<val:
smalles=a[i]
print("Minimum is",smalles)
def lge():
val=a[0]
for i in range(len(a)):
if a[i]>val:
larges=a[i]
print("Maximum is",larges)
lge()
sml()
Upvotes: 0
Views: 95
Reputation: 71517
In these loops:
val=a[0]
for i in range(len(a)):
if a[i]>val:
larges=a[i]
you have two values, val
and largest
, when you really only want one (largest
). The idea of finding a maximum value is that you want to be comparing against the largest value you've found so far, and constantly updating it each time you find a larger value.
A much shorter way of writing this code would be:
a = []
while True:
val = input()
if val == "done":
break
try:
a.append(int(val))
except ValueError:
print("Invalid")
print(max(a))
print(min(a))
Or, if you weren't allowed to use the max
and min
functions, you could simply maintain largest
and smallest
as you go, instead of keeping an array at all:
largest = None
smallest = None
while True:
val = input()
if val == "done":
break
try:
num = int(val)
except ValueError:
print("Invalid")
continue
if (largest, smallest) == (None, None):
largest, smallest = num, num
if largest < num:
largest = num
if smallest > num:
smallest = num
print(largest)
print(smallest)
Upvotes: 0
Reputation: 19
In every iteration you are comparing with first element of list i.e val (val = a[0]) and not updating the val . Modify
a=list()
while True:
sval=input()
if sval == "done":
break
try:
value=int(sval)
a.append(value)
print("Appended")
except:
print("Invlid")
continue
def sml():
print(a)
smalles=a[0]
for i in range(len(a)):
if smalles > a[i]:
print(a[i] , end=" ")
smalles=a[i]
print()
print("Minimum is",smalles)
def lge():
larges=a[0]
for i in range(len(a)):
if a[i]>larges:
larges=a[i]
print("Maximum is",larges)
lge()
sml()
Upvotes: 0
Reputation: 1375
while
loopBelow is smallest possible changed answer from your code.
a=list()
while True:
sval=input()
if sval == "done":
break
try:
value=int(sval)
a.append(value)
except:
print("Invlid")
continue
def sml():
smalles=a[0]
for i in range(len(a)):
if a[i]<smalles:
smalles=a[i]
print("Minimum is",smalles)
def lge():
larges=a[0]
for i in range(len(a)):
if a[i]>larges:
larges=a[i]
print("Maximum is",larges)
lge()
sml()
Upvotes: 1
Reputation: 1841
Instead of finding the maximum and minimum value after appending to the array, you can find it before. This will reduce the cost of iterating the array each and every time to find the maximum and minimum.
a=list()
max_, min_ = float('-inf'), float('inf')
while True:
sval=input()
if sval == "done":
break
try:
value = int(sval)
if value > max_:
max_ = value
elif value < min_:
min_ = value
a.append(value)
except:
print("Invalid")
continue
print("Maximum is", max_)
print("Minimum is", min_)
Upvotes: 1