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:
  #1  
Old March 3rd, 2004, 08:27 PM
reaper69 reaper69 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 30 reaper69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
How can I loop a program to restart after it is finish???

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

Reply With Quote
  #2  
Old March 3rd, 2004, 09:04 PM
reaper69 reaper69 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 30 reaper69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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!

Reply With Quote
  #3  
Old March 3rd, 2004, 09:51 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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 09:58 PM.

Reply With Quote
  #4  
Old March 4th, 2004, 06:02 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,536 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 18 h 2 m 16 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
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


Reply With Quote
  #5  
Old March 4th, 2004, 07:47 AM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the new info . So basicaly you should always use raw_input() ?

Reply With Quote
  #6  
Old March 4th, 2004, 07:55 AM
reaper69 reaper69 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 30 reaper69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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



Reply With Quote
  #7  
Old March 4th, 2004, 08:29 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,536 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 18 h 2 m 16 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
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.

Reply With Quote
  #8  
Old May 6th, 2004, 09:40 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 58 sec
Reputation Power: 5
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

Reply With Quote
  #9  
Old May 6th, 2004, 10:19 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,536 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 18 h 2 m 16 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 the? Your program is exactly the same as Xx's except that it uses the int(raw_input('prompt')) form detailed above

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How can I loop a program to restart after it is finish???


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 1 hosted by Hostway