SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old January 27th, 2004, 11:21 AM
lucas's Avatar
lucas lucas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: UK
Posts: 367 lucas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 30 sec
Reputation Power: 5
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.

Reply With Quote
  #2  
Old January 27th, 2004, 11:31 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old January 27th, 2004, 12:06 PM
lucas's Avatar
lucas lucas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: UK
Posts: 367 lucas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 30 sec
Reputation Power: 5
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 12:11 PM.

Reply With Quote
  #4  
Old January 27th, 2004, 12:16 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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 12:18 PM.

Reply With Quote
  #5  
Old January 27th, 2004, 12:25 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Quote:
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.

Reply With Quote
  #6  
Old January 27th, 2004, 12:46 PM
lucas's Avatar
lucas lucas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: UK
Posts: 367 lucas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 30 sec
Reputation Power: 5
Iv got it workin, tnx all.

Reply With Quote
  #7  
Old January 27th, 2004, 01:45 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
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.

Reply With Quote
  #8  
Old January 27th, 2004, 06:16 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
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).

Reply With Quote
  #9  
Old January 28th, 2004, 03:05 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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 03:15 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Loops


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway