Thread: Loops

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

    Join Date
    Jan 2004
    Location
    UK
    Posts
    367
    Rep Power
    11

    Loops


    OK i've written my program, but instead of having it exit i want it to rune again and again for an infinite number of times, or until someone types quit or ext etc. How would I go about this, im guessing it involves loops but iv tried a few things and they dont work.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    What you're looking for here is a while True loop, luckily not hard check it out!

    Code:
    while True:
        ...
        code here
        ...
        if typed == 'quit':
            ...
            break
            ...
    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    UK
    Posts
    367
    Rep Power
    11
    OK this is the code:

    Code:
    #!/usr/bin/env python
    while true:
    import sys, time, os, urllib
    choice = raw_input('Type (news) for news or (web)to visit the website (forum) to go to the forum.')
    if choice == 'news':
        for each in range(5):
    	sys.stdout.write('[' + '=' * each + ']\r')
    	time.sleep(0.2)
        page = urllib.urlopen('http://www.geocities.com/hlh_halo/news.txt').read()
        file('news.txt', 'w').write(page)
        os.system('news.txt')
        print
    elif choice == 'web':
        for each in range(5):
    	sys.stdout.write('[' + '=' * each + ']\r')
    	time.sleep(0.2)
        os.system('explorer http://www.geocities.com/hlh_halo')
        print
    elif choice == 'forum':
        for each in range(5):
    	sys.stdout.write('[' + '=' * each + ']\r')
    	time.sleep(0.2)
        os.system('explorer http://lucas900.proboards18.com/index.cgi')
        print 
    if typed == 'quit':
        break
    and the error message I get about the command 'import' at the top is that an indent was expected. What are indents and how should I fix this problem?
    Many thanks.
    Last edited by netytan; January 27th, 2004 at 01:11 PM.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Indents are very important to Python, since there used to denote where blocks start and finish! Definatly dont forget to indent code of you're programs jsut wont work

    Soooo.... you're program should end up something like this (untested).

    Code:
    #!/usr/bin/env python
    
    import sys, time, os, urllib
    
    while True:
    	choice = raw_input('Type (news) for news, (web) to visit the website, (forum) to go to the forum or (quit) to quit.')
    	if choice == 'news':
    		for each in range(5):
    			sys.stdout.write('[' + '=' * each + ']\r')
    			time.sleep(0.2)
    			page = urllib.urlopen('http://www.geocities.com/hlh_halo/news.txt').read()
    			file('news.txt', 'w').write(page)
    			os.system('news.txt')
    			print
    	elif choice == 'web':
    		for each in range(5):
    			sys.stdout.write('[' + '=' * each + ']\r')
    			time.sleep(0.2)
    			os.system('explorer http://www.geocities.com/hlh_halo')
    			print
    	elif choice == 'forum':
    		for each in range(5):
    			sys.stdout.write('[' + '=' * each + ']\r')
    			time.sleep(0.2)
    			os.system('explorer http://lucas900.proboards18.com/index.cgi')
    			print 
    	elif choice == 'quit':
    		break
    Note: don't import inside loops, this can cause major preformance problems on occasion.

    Mark.
    Last edited by netytan; January 27th, 2004 at 01:18 PM.
    programming language development: www.netytan.com Hula

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

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    Originally posted by netytan
    Note: don't import inside loops, this can cause major preformance problems on occasion.
    Meta-note: I'm pretty sure this isn't true. Not at my machine right now but I can check it out later on and respond with a definite answer.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    UK
    Posts
    367
    Rep Power
    11
    Iv got it workin, tnx all.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally posted by Strike
    Meta-note: I'm pretty sure this isn't true. Not at my machine right now but I can check it out later on and respond with a definite answer.
    Python is optomized so it doesn't import the same module more than once - you have to call reload(name) to reload a module - but i've read a number of articles on improving preformance and this has come up more than once.

    This may have been fixed, but since i havn't heard anything i'd assume it hasnt.

    so make of it what you will Strike , just not the best idea! and what would be the point anyway?

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

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

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    I didn't say it was a good idea I just don't think it'll be a performance hit (or at least, not a huge one).
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Yeah i can see that, i mean, if Python is designed not to import a module more than once then there cant be much of a hit. But hey work with what you've heard .

    Take care dude.

    Mark.
    Last edited by netytan; January 28th, 2004 at 04:15 AM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo