I do not see any recursion.
pyTony
pyMod
6,330 posts since Apr 2010
Reputation Points: 879
Solved Threads: 989
Skill Endorsements: 27
Recursion calls the function from within the function with an adjusted parameter.
Example:
def remove_char_right(mystr, num, count=0):
"""
remove num of characters to the right of a string
"""
print("{} {}".format(count, mystr)) # test
# exit condition, return the final result
if count >= num:
return mystr
mystr = mystr[:-1]
# needs return
# function calls itself
# notice the adjusted parameters mystr and count
return remove_char_right(mystr, num, count+1)
# test it
mystr = 'parameter'
num = 5
newstr = remove_char_right(mystr, num)
print('-'*15)
print(newstr)
''' output -->
0 parameter
1 paramete
2 paramet
3 parame
4 param
5 para
---------------
para
'''
Lardmeister
Posting Virtuoso
1,940 posts since Mar 2007
Reputation Points: 465
Solved Threads: 73
Skill Endorsements: 5
Put in a few test print() to catch the many errors.
sneekula
Nearly a Posting Maven
2,483 posts since Oct 2006
Reputation Points: 1,000
Solved Threads: 231
Skill Endorsements: 2
Notice that a while loop mimics a recursive function rather closely. It should give you hints how to set it up ...
''' maxnum1.py
find the highest number in a list
not using Python's max(list) function
'''
mylist = [1, 2, 3, 4, 5 ,6, 7, 3, 5]
maxnum = 0
index = 0
while True:
num = mylist[index]
# update maxnum to be the highest number
if num > maxnum:
maxnum = num
# go to next index
index += 1
# exit condition
if index >= len(mylist):
break
print(maxnum) # 7
# test
print(max(mylist)) # 7
Actually, while loops are faster then the high overhead recursive functions.
vegaseat
DaniWeb's Hypocrite
6,499 posts since Oct 2004
Reputation Points: 1,451
Solved Threads: 1,618
Skill Endorsements: 37