2 Replies - 3433 Views - Last Post: 24 March 2010 - 12:29 PM Rate Topic: -----

#1 mapexx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-November 09

PYTHON - using a function with a range

Posted 23 March 2010 - 08:39 PM

I need to embed my function hailstone() in a for loop that will print output for numbers in a certain range that I hard code. Here is my function:

def hailstone():
    numbers = []
    num = input("Enter a number: ")
    while num != 1:
        numbers.append(num)
        if isOdd(num):
            num = 3 * num + 1
        else:
            num /= 2
        if num != 1:
            print num,"->",
        else:
            print num,

    length = len(numbers)
    print "; length = ",length



Right now it is prompting for user input inside the function(I don't know if this is the correct way to do it or not). I want to just test it with a range of values that I hardcode, such as 5 to 10. Eventually the starting and ending number in the range need to be entered by the user. This is what I have so far, I cannot think how to get it to work.
def main():
    num = 0
    for num in range(5, 10):
        hailstone()

main()


It just prompts me for a number 4 times, I need it to automatically print output for numbers 5 through 10. Any help is very much appreciated, thank you!

This post has been edited by mapexx: 23 March 2010 - 08:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: PYTHON - using a function with a range

#2 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: PYTHON - using a function with a range

Posted 24 March 2010 - 05:40 AM

You need to get rid of the num = input line, and when you define hailstone, you should do def hailstone(num) so it takes an argument. Then in main, do x = range (5,10), and hailstone(x). You'll need to do some re-working of the hailstone function, but that should be a start.

Is this Python 2.6 or 3.1?
Was This Post Helpful? 1
  • +
  • -

#3 mapexx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-November 09

Re: PYTHON - using a function with a range

Posted 24 March 2010 - 12:29 PM

View Postchemicalfan, on 24 March 2010 - 04:40 AM, said:

You need to get rid of the num = input line, and when you define hailstone, you should do def hailstone(num) so it takes an argument. Then in main, do x = range (5,10), and hailstone(x). You'll need to do some re-working of the hailstone function, but that should be a start.

Is this Python 2.6 or 3.1?

Thank you for your help. And this is 3.1
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1