I am trying to write a tree growing algorithm where Trees go through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter.
The problem I have is that Now, a new Tree is planted at the onset of spring. Its height is 1 meter. I want to find the height of the tree after N growth cycles?
I was doing some research on recursive functions where the function is calling it self. I here it makes the code you write more elegant and simple then while loops. I am having problems execute this function though
n = input('How long would you like the tree to for?: ')
def cycle(n):
if n == 0:
n = + 1
return n
print '/n' # The reason for all the '/n' is just for neatness.
print('The tree will be ' + n + 'Ft tall')
elif n % 2 == 0:
n = 1 + cycle(n - 1)
return n
print '/n'
print('The tree will be ' + n + 'Ft tall')
elif n % 2 != 0:
n = 2 * cycle(n - 1)
return n
print '/n'
print('The tree will be ' + n + ' Ft tall')
cycle(n)
I am new to programing so any help at all would greatly be appreciated.
-ab intra
Aucun commentaire:
Enregistrer un commentaire