gpt4 book ai didi

python - 如何打印返回值?

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:20 25 4
gpt4 key购买 nike

**如何打印返回index1,index2?我尝试了不同的方法,但没有打印出任何内容。**

class Solution:
def twoSum(self, nums, target):
nums = [2,7,11,15]
target = 9
hash_map = {}
for index, value in enumerate(nums):
hash_map[value] = index
for index1, value in enumerate(nums):
if target - value in hash_map:
index2 = hash_map[target - value]
if index1 != index2:
return [index1,index2]

最佳答案

class Solution:
def twoSum(self, nums, target):
nums = [2,7,11,15]
target = 9
hash_map = {}
for index, value in enumerate(nums):
hash_map[value] = index
for index1, value in enumerate(nums):
if target - value in hash_map:
index2 = hash_map[target - value]
if index1 != index2:
return [index1,index2]

if __name__ == '__main__':
print(Solution().twoSum(9, [2,7,11,15]))

我认为您正在寻找的是一个 main 函数,您可以在其中使用您的函数

关于python - 如何打印返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46219622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com