|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How can I loop a program to restart after it is finish???
How can I loop a program to restart after it is finish???
|
|
#2
|
|||
|
|||
|
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! |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
Thanks for the new info
. So basicaly you should always use raw_input() ? |
|
#6
|
|||
|
|||
|
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 ![]() |
|
#7
|
||||
|
||||
|
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. |
|
#8
|
|||
|
|||
|
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
|
|
#9
|
||||
|
||||
|
What the? Your program is exactly the same as Xx's except that it uses the int(raw_input('prompt')) form detailed above
![]() Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > How can I loop a program to restart after it is finish??? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|