show position of highest and lowest value on a list without using any methods Python
Question:
hello I need to create a list that will show the position where it has the highest and lowest value typed by the user but without using any methods.
I already did half of it but when it comes to show the position I’m not sure how to do this without using a function. Any ideas?
listone = [0] * 10
for c in range (0, 10):
listone[c] = (int(input('Type 10 values: ')))
if c == 0:
high = low = listone[c]
else:
if listone[c] > high:
high = listone[c]
if listone[c] < low:
low = listone[c]
print(f'The highest value typed was {high} and the lowest '
f'was {low}')
Answers:
If you don’t want to use python’s inbuilt function for returning the index, then either you manually put a for loop in its place, or put that for loop in a user-defined function for modularity’s sake, just like I have.
listone = [0] * 10
feed_in = [23, 43, 1, 11, 90, 98, 23, 32, 43, 54] # Im sparing myself the effort of manually inputting
for c in range (0, 10):
# listone[c] = (int(input('Type 10 values: ')))
listone[c] = feed_in[c]
if c == 0:
high = low = listone[c]
else:
if listone[c] > high:
high = listone[c]
if listone[c] < low:
low = listone[c]
def find_index(array, element):
for iters in range(len(array)):
if array[iters] == element:
return iters
print(f'The highest value typed was at {find_index(listone, high)} and the lowest was {find_index(listone, low)}')
This works for me:
import random
random_numbers = random.sample(range(0, 100), 10)
highest_score = 0
for index, value in enumerate(random_numbers):
if value > highest_score:
highest_score = value
highest_index = index
print(highest_index, highest_score)
print(random_numbers)
hello I need to create a list that will show the position where it has the highest and lowest value typed by the user but without using any methods.
I already did half of it but when it comes to show the position I’m not sure how to do this without using a function. Any ideas?
listone = [0] * 10
for c in range (0, 10):
listone[c] = (int(input('Type 10 values: ')))
if c == 0:
high = low = listone[c]
else:
if listone[c] > high:
high = listone[c]
if listone[c] < low:
low = listone[c]
print(f'The highest value typed was {high} and the lowest '
f'was {low}')
If you don’t want to use python’s inbuilt function for returning the index, then either you manually put a for loop in its place, or put that for loop in a user-defined function for modularity’s sake, just like I have.
listone = [0] * 10
feed_in = [23, 43, 1, 11, 90, 98, 23, 32, 43, 54] # Im sparing myself the effort of manually inputting
for c in range (0, 10):
# listone[c] = (int(input('Type 10 values: ')))
listone[c] = feed_in[c]
if c == 0:
high = low = listone[c]
else:
if listone[c] > high:
high = listone[c]
if listone[c] < low:
low = listone[c]
def find_index(array, element):
for iters in range(len(array)):
if array[iters] == element:
return iters
print(f'The highest value typed was at {find_index(listone, high)} and the lowest was {find_index(listone, low)}')
This works for me:
import random
random_numbers = random.sample(range(0, 100), 10)
highest_score = 0
for index, value in enumerate(random_numbers):
if value > highest_score:
highest_score = value
highest_index = index
print(highest_index, highest_score)
print(random_numbers)