我有一个程序来生成 1-5 之间的 10 个随机数的列表,然后计算每个数字出现的次数,然后创建一个删除重复项的新列表。我不断得到某些全局名称没有定义。我似乎对返回函数感到困惑。不过我需要这种格式,所以我不能把打印语句放在每个函数的末尾。有什么帮助吗?
def main():
"""print output of program"""
firstList= []
randomTen(firstList)
countInts(firstList)
removeDuplicates(firstList)
print(firstList)
print('The number of times one appears is', ones)
print('The number of times two appears is', twos)
print('The number of times three appears is', threes)
print('The number of times four appears is', fours)
print('The number of times five appears is', fives)
print(secondList)
def randomTen(firstList):
"""return list of ten integers"""
for num in range(1,11):
x= int(random.randint(1,5))
firstList.append(x)
return firstList
def countInts(firstList):
"""count the number of times each integer appears in the list"""
ones= firstList.count(1)
twos= firstList.count(2)
threes= firstList.count(3)
fours= firstList.count(4)
fives= firstList.count(5)
return ones, twos, threes, fours, fives
def removeDuplicates(firstList):
"""return list of random integers with the duplicates removed"""
secondList=set(firstList)
return secondList