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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Python Help (basic)

  • 28-03-2014 9:05pm
    #1
    Registered Users, Registered Users 2 Posts: 1,453 ✭✭✭


    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, Registered Users 2 Posts: 3,323 ✭✭✭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