作业帮 > 英语 > 作业

Write a program which allows the user to enter a list of num

来源:学生作业帮 编辑:作业帮 分类:英语作业 时间:2024/07/13 13:55:34
Write a program which allows the user to enter a list of numbers.
Write a program which allows the user to enter a list of numbers,then works out whether there are any two numbers in the list which sum to 100.Hint:you could loop through the numbers in the list,and for each number,loop through the list again looking for a number that adds to it to make 100.However,there are other ways to do this too.
def checkio(data, target):
data.sort()
i = 0
j = len(data)-1
while(i < j):
if data[i] + data[j] > target:
j -= 1
elif data[i] + data[j] < target:
i += 1
else:
return str(data[i]) + ' + ' + str(data[j]) + ' = ' + str(target)
return 'No solution!'

if __name__ == '__main__':
data = [10,2,3,87,11,1,2,41,34,56,44,93,5,16,74,23,22,19]
target = 100;
print data
print checkio(data,target)