我对Python很新,需要帮助。我已经在网上搜索了几天,这有助于修复以前的错误,但我仍然坚持这一点。我知道这已被问过几次,我已经阅读了论坛;但是,我仍然无法解决这个问题。
我正在使用Python 3.4.4
此作业的目标是创建一个包含范围为1-100的50个数字的随机列表并打印出来。然后你必须采取相同的列表并删除重复的数字,并打印该列表。最终,应该打印2个列表,一个列表重复,一个列表没有。这是我的代码:
from random import randrange
def createList():
print("A program that will generate a list of 50 random numbers then remove any duplicates.")
for i in range(50):
randList = randrange(101)
print(randList)
def removeDuplicates(randList):
uniqueList = []
for i in randList:
if i not in uniqueList:
uniqueList.append(i)
print(uniqueList)
def main():
createList()
removeDuplicates()
if __name__ == "__main__": main()
这是我的错误:
Traceback (most recent call last):
File "C:/Python34/Python Assignments/test-randNumbs.py", line 20, in <module>
if __name__ == "__main__": main()
File "C:/Python34/Python Assignments/test-randNumbs.py", line 18, in main
removeDuplicates()
TypeError: removeDuplicates() missing 1 required positional argument: 'randList'
答案 0 :(得分:1)
首先,您需要将列表传递给removeDuplicates()
函数。但是,要做到这一点,您还需要在createList()
。
from random import randrange
def create_list():
rand_list = []
print("A program that will generate a list of 50 random numbers then remove any duplicates.")
for i in range(50):
rand_list.append(randrange(101))
print(rand_list)
return rand_list
def remove_duplicates(rand_list):
unique_list = []
for i in rand_list:
if i not in unique_list:
unique_list.append(i)
print(unique_list)
def main():
rand_list = create_list()
print('unique list:')
remove_duplicates(rand_list)
if __name__ == "__main__": main()