当input1 = [1, 2, 3, 4, 5]
input2 = [2, 2]
head_tail_compare(input1),
这段代码输出结果是(False, 5),
请问有什么办法让它变成:
False
5
这样吗
def head_tail_compare(lst):
"""
A function that compares the first element and the last element in the
input list.
---
Parameters:
lst: a list of numbers
---
Print True if they are the same, False if not
Return the larger element
>>> input1 = [1, 2, 3, 4, 5]
>>> input2 = [2, 2]
>>> head_tail_compare(input1)
False
5
>>> head_tail_compare(input2)
True
2
"""
# YOUR CODE GOES HERE #
if lst[0]==lst[-1]:
return True
if lst[0]!=lst[-1] and lst[0]>lst[-1]:
return False,lst[0]
if lst[0]!=lst[-1] and lst[0]<lst[-1]:
return False,lst[-1]
return
用一个变量接受在遍历输出即可
def head_tail_compare(lst):
# YOUR CODE GOES HERE #
if lst[0]==lst[-1]:
return True
if lst[0]!=lst[-1] and lst[0]>lst[-1]:
return False,lst[0]
if lst[0]!=lst[-1] and lst[0]<lst[-1]:
return False,lst[-1]
return
a=head_tail_compare( [1, 2, 3, 4, 5])
for i in a:
print(i)
以字符串的形式输出即可,如:return 'False' + '\n' + str(lst[0])
def head_tail_compare(lst):
# YOUR CODE GOES HERE #
if lst[0]==lst[-1]:
return 'True'
if lst[0]!=lst[-1] and lst[0]>lst[-1]:
return 'False' + '\n' + str(lst[0])
if lst[0]!=lst[-1] and lst[0]<lst[-1]:
return 'False' + '\n' + str(lst[-1])
return
input1 = [1, 2, 3, 4, 5]
input2 = [2, 2]
print head_tail_compare(input1)
输出为:
>>>
False
5
>>>
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!