我想编写一个程序给用户一些数字,然后如果列表中的3个数字的总和== 0 prit是:我的代码的一部分:
import os
from typing import Mapping
from colorama import Fore , init
init()
os.system("cls" or "clear")
clear = lambda: os.system('cls')
# GIVE LIST NUMBER
listed = input(Fore.GREEN+"Please Enter A List Of Number: ")
# CONVERT INPUT(STR) TO LIST:
listed = list(map(int, listed.split(',')))
listed= list(map(int, listed))
#CHEACK SUM IN LIST == 0
我不知道如何检查我的IF以打印是或否.....
例:
列表= [3,2,-2,-5]
以3 + 2-5 = 0打印YES
我想你想要这样的东西:
import os
from typing import Mapping
from colorama import Fore , init
init()
os.system("cls" or "clear")
clear = lambda: os.system('cls')
# GIVE LIST NUMBER
listed = input(Fore.GREEN+"Please Enter A List Of Number: ")
# CONVERT INPUT(STR) TO LIST:
listed = list(map(int, listed.split(',')))
listed= list(map(int, listed))
#CHEACK SUM IN LIST == 0
if sum(listed) == 0:
print("yes")
更新(根据您的评论):
from itertools import combinations
list1 = [3, 2]
list2 = [3, 2, -2, -5]
list3 = [5, -5, 0]
def check_sum(list_of_numbers):
if len(list_of_numbers) >= 3:
subsets = list(combinations(list_of_numbers, 3))
for each_subset in subsets:
if sum(each_subset) == 0:
return "Yes"
return "No"
else:
return "No"
print(check_sum(list1))
print(check_sum(list2))
print(check_sum(list3))
输出:
No
Yes
Yes
一些积分到https://www.geeksforgeeks.org/python-program-to-get-all-subsets-of-given-size-of-a-set/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句