【发布时间】:2014-12-12 03:36:08
【问题描述】:
我是编程初学者,我正在尝试创建一个程序,在不使用“max”函数的情况下,在 1000 个随机数的列表中找到最大的数字,然后在列表中找到最大的数字位置,不使用“索引”功能(我将数字设置为 0-10,以便确保程序正常运行)。到目前为止,我的程序可以正常工作。有时,它会显示位置,当它显示时,它会显示错误的位置,而其他时候,它会显示一个错误,指出索引超出范围。有人可以帮忙吗?
import random
num_list = []
for num in range(10):
num_list.append(random.randrange(0,11))
max_num = -1
for num in num_list:
if num > max_num:
max_num = num
location=num_list[max_num]
print "The computer entered: " + str(num_list)
print "The largest number in this list is: " + str(max_num) + " The location is: " + str(location)
【问题讨论】:
标签: python for-loop random indexing max