I have no idea how to get started

-Prompt user for an integer
-If the number is even, divide it in half
- If the number is odd, multiply it by 3 then add 1
- If the number reaches 1, stop; otherwise go to step 1
-Use while loop.
-Negative number to quit.

Here's what I did so far.
Obviously, there are syntax errors and I don't know the parity code.

numStr = raw_input("Please enter an integer: ")
number = int(numStr)
while number !=1:
if number == even:
number = number/2
elif if number == odd:
number = (number * 3) + 1
elif if number == 1
break

print 'Done'

Please wrap you code in [ code=python ] [ /code ] tags next time.
Your algorithm seems fine according to the pseudo code you gave above. So I'm ging to show you how to determine if an integer is odd or even.

numStr = raw_input("Please enter an integer: ")
number = int(numStr)
while number > 0 and number != 1:
    if number % 2:
        number = number * 3 + 1
    else:
        number =/= 2

print "done"

Things to note:

I removed the last elif because the while loop already checks for that. I also now check that number is not negative.

The % operator means modulus. It divides a number by its operand and returns the remainder. Using % 2 will be 0 when a number is even (evaluates as False) and 1 when it is odd (evaluates as True)

Cool thanks, but I get a syntax error on

else:
number =/=2

Woops that should be number /= 2. It's shorthand for number = number / 2.

huh....I just get 'done' for any integers. I'm not sure if that's what's supposed to happen

From the pseudo code in the first post, it looks like the algorithm is supposed to reduce any integer to 1. If that is happening then it will indeed print "done" at the end.

If you need to print the amount of steps it took, create a variable named steps and assign zero to it, then increment it by one in the while loop.

Unless there's something else you want it to do that I missed?

I've just pmed you.

So you want to print number out everytime you enter the loop. You can use a comma to get them to appear on the same line.

You want to keep prompting until a negative number is entered?
Take out the > 0 check i entered, wrap the first while loop in a new while loop that does this check. Then prompt for number again at the end of that new while loop.

all right works now. Thanks!!!

Member Avatar for leegeorg07

after looking at your code i made my own i a function which i feel is faster, i also added in a print for a counter, the number and the final number. I used Python30.

def hailstone(num):
    i = 0
    while num > 1:
        if num <= 0:
            print("please enter a new number")
        elif num %2:
            num = (num * 3) + 1
        else:
            num  /=2
        print(num)
        i+=1
    print()
    print(i)
    print()
    print("done")
    print()
    print(num)

im going to time it to make sure but thats my first idea.

Member Avatar for leegeorg07

no i was wrong, it turns out that scru's program is 0.0010001659393000014 seconds faster than mine with the number 10000000003. but i thought it was a good exercise

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.