[英]show position of highest and lowest value on a list without using any methods Python
您好,我需要创建一个列表,显示 position,其中它具有用户键入的最高和最低值,但不使用任何方法。
我已经完成了一半,但是在展示 position 时,如果不使用 function,我不确定如何做到这一点。 有任何想法吗?
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}')
如果您不想使用 python 的内置 function 来返回索引,那么您可以手动放置一个 for 循环,或者将 for 循环放在用户定义的 function 中,就像我一样。
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)}')
这对我有用:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.