Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Python Help (basic)

Options
  • 28-03-2014 10:05pm
    #1
    Registered Users Posts: 1,433 ✭✭✭


    Hi,

    I am stuck on a python exercise that has me stumped. What I want to do is get the user to enter a number and then generate a 'collatz sequence' based on this. Then I want to put this sequence into a list.

    I have the first bit ok (I think)

    x = int(input())
    while x != 1:
    if x % 2 == 0:
    x = x/2
    print(x)
    else:
    x =3*x + 1
    print(x)

    So if I enter 3,

    10
    5.0
    16.0
    8.0
    4.0
    2.0
    1.0

    will appear on the screen.

    But how do I get these into a list?


Comments

  • Registered Users Posts: 3,287 ✭✭✭padraig_f


    [B]sequence = []
    [/B]x = int(input())
    while x != 1:
       if x % 2 == 0:
          x = x/2
          print(x)
       else:
          x =3*x + 1
          print(x)
       [b]sequence.append(x)[/b]
    
    [B]print 'sequence: {0}'.format(sequence)
    [/B]
    

    Added lines in bold.


Advertisement