View Single Post
  #1 (permalink)  
Old 11-02-2007, 08:00 AM
ViewSonic20061 ViewSonic20061 is offline
Novice
Join Date: Nov 2007
Posts: 1
iTrader: (0)
ViewSonic20061 is on a distinguished road
Help with Python

Does anyone know how to make the program running until the ouput becomes 1? If hail(x) = x/2 if x is even(i.e., x%2 is 0) and hail(x) = 3x+1 if x is odd(i.e., x%2 is 1)

example:
Please enter a starting value: 3
Output will be:
3
10
5
16
8
4
2
1

so far wat i came up with was:
Code:
x =  int(raw_input("Please enter a starting value:"))

def hail(x):
    if x % 2 == 0:
        return x / 2
    else:
        return (3*x)+1
output = hail(x)
print output
but i still cant seem to think of a way to make them repeat itself and keep caculating till it becomes one. Any ideas or suggestions?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by TeraTask : 11-03-2007 at 02:34 PM. Reason: Added code tags
Reply With Quote