学习递归,错误消息(默认参数后接非默认参数)python [英] Learning recursion, error message(non-default argument follows default argument) python
问题描述
尝试练习学习递归.该程序旨在遍历列表并打印出所选字母的位置.我也不明白此错误消息的含义:(非默认参数紧随默认参数之后).不太了解为什么有错误或如何解决它.任何帮助总是感激不尽,谢谢.
Trying to practice learning recursion. The program is designed to go through the list and print out the location of the chosen letter. I also don't understand what this error message means:(non-default argument follows default argument). Not really understanding why there is an error or how to fix it. Any help always appreciated, thanks.
def get_position(pos = 0,List,letter = 'o'):
if pos < 0:
pos = 0
if pos == len(List):
return None
if List[pos] is letter:
return pos
else:
return get_position(pos +1,List,letter)
print('position: ',get_position(0,'hello','o'))
推荐答案
问题出在此行:
get_position(pos = 0,List,letter = 'o')
请注意,您在默认参数pos = 0
之后提供了List
参数,这是一个非默认参数.这是不允许的.尝试重新排列参数:
Notice that you give the List
argument, a non-default argument, after default argument pos = 0
. This is not allowed. Try reordering your arguments:
get_position(List, pos = 0,letter = 'o')
要理解为什么不允许这样做,请考虑以下问题:如何调用get_position
并传递 second 参数而不传递 first 参数?您可以想到某种语言可以支持这一点的方式,例如允许像get_position( , myList)
这样的调用,但是据我所知,没有语言可以支持.
To understand why this is not allowed, think about this: how would you call get_position
and pass in the second argument without passing in the first argument? You can think of some ways a language could support this, such as allowing calls like get_position( , myList)
, but to my knowledge there is no language that does.
这篇关于学习递归,错误消息(默认参数后接非默认参数)python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!