Calculating a sequence of numbers using the while loop in Python -


i'm trying return sequence of numbers using while loop, starting input value num , ending 1. example:

>>> tray(8) [8, 2, 1] 

if number even, i'd replace num integer value of num**0.5, , if it's odd should replace num integer value of num**1.5.

def tray(num):     '''returns sequence of numbers including starting     value of num , ending value of 1, replacing num     integer value of num**0.5 if , num**1.5 if odd'''     while num != 1:         if num %2 == 0:             num**=0.5         else:             num**=1.5         return num 

i'm sort of lost on how make sure replacements integers - if try int(num**0.5) comes "invalid syntax." additionally, it's returning answer of num**0.5 , can't figure out how return starting value num along sequence 1. input.

these adjustments fix errors in code

def tray(num):     '''returns sequence of numbers including starting     value of num , ending value of 1, replacing num     integer value of num**0.5 if , num**1.5 if odd'''     seq = [ num ]     while num != 1:         if num %2 == 0:             num = int(num**0.5)         else:             num = int(num**1.5)          seq.append( num )      return seq 

here rewritten generator.

def tray(num):     '''returns sequence of numbers including starting     value of num , ending value of 1, replacing num     integer value of num**0.5 if , num**1.5 if odd'''     yield  num     while num != 1:         if num %2 == 0:             num = int(num**0.5)         else:             num = int(num**1.5)          yield num 

which can used create list this.

list( tray(8) ) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -