#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    30
    Rep Power
    11

    How can I loop a program to restart after it is finish???


    How can I loop a program to restart after it is finish???
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    30
    Rep Power
    11
    ok this is my program

    (Code


    l = int(raw_input('Enter a number from 100 and 1000000)
    n = 1

    while n <= l:
    print n
    n = n * 2


    now I just want to make it so it loops the program so the person messing with it can have hours of fun just being an idiot and watch the computer double till it gets to the number they selected!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    This should work:

    Code:
    while True:
    	l = input('Enter a number from 100 and 1000000 (0 to exit): ')
    	n = 1
    	
    	if l == 0:
    		break
    
    	while n <= l:
    		print n
    		n = n * 2
    You don't need to use raw_input if your just getting integers. You can get rid of the if I added if you don't need I way to stop it...
    Last edited by XxChris; March 3rd, 2004 at 10:58 PM.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Actually i have to admit that input scares me a little; since, strike explained how eval() could be used to royaly distroy a computer by executing arbitary code . This applies because input() is the same as doing eval(raw_input()).

    Code:
    >>> os = __import__("os")
    >>> os.getcwd()
    'C:\\Python23'
    >>> a = input('__import__("os")')
    __import__("os")__import__("os")
    >>> a.getcwd()
    'C:\\Python23'
    >>>
    I know this is a mood point since this porgram is so small and just for fun ect. but worth remembering. Admitadly i have used it in the past. but before warned is before armed .

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Thanks for the new info . So basicaly you should always use raw_input() ?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    30
    Rep Power
    11
    Thanx both of you I found out what I needed and I found out that input() shouldn't be used raw_input() should. So the program should look like this.


    while True:
    l = int(raw_input('Enter a number from 100 and 1000000 (0 to exit): ')
    n = 1

    if l == 0:
    break

    while n <= l:
    print n
    n = n * 2


  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Personally i don't really use input() for anything. If i want the data eval()-uated i do it manually . Of course its up to you though!

    You can make you're code appear indented on here by surounding your code with CODE tags or highlighting your code and clicking the # button.

    Take care guys,

    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    57
    Rep Power
    11
    Still wrong

    Here is the correct code

    Code:
    while True:
     l = int(raw_input('Enter a number from 100 and 1000000 (0 to exit): '))
     n = 1
     if l == 0:
      break
     while n<=l:
      print n
      n= n * 2
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    What the? Your program is exactly the same as Xx's except that it uses the int(raw_input('prompt')) form detailed above

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo