The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Loops
Discuss Loops in the Python Programming forum on Dev Shed. Loops Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 27th, 2004, 11:21 AM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: UK
Posts: 367
Time spent in forums: 1 Day 6 h 7 m 45 sec
Reputation Power: 10
|
|
|
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.
|

January 27th, 2004, 11:31 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

January 27th, 2004, 12:06 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: UK
Posts: 367
Time spent in forums: 1 Day 6 h 7 m 45 sec
Reputation Power: 10
|
|
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.
|

January 27th, 2004, 12:16 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

January 27th, 2004, 12:25 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
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.
|

January 27th, 2004, 12:46 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: UK
Posts: 367
Time spent in forums: 1 Day 6 h 7 m 45 sec
Reputation Power: 10
|
|
|
Iv got it workin, tnx all.
|

January 27th, 2004, 01:45 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

January 27th, 2004, 06:16 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
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).
|

January 28th, 2004, 03:05 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|