|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
Quote:
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. |
|
#6
|
||||
|
||||
|
Iv got it workin, tnx all.
|
|
#7
|
||||
|
||||
|
Quote:
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. |
|
#8
|
|||
|
|||
|
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). |
|
#9
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Loops |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|