Hello again!

I have a new question.

I have run a script and wants to run it again with a "keypress" input from the user, how do i code that. Like this:

import subprocess
drive=input(" If you want to start the script again , press 1: ")
if drive == 1:
subprocess.call(["programname"])

the above can be used to start a external program from inside a python script but if i only wants to run the same script again? I use "idle" when i code python, so i guess the script must open up again inside idle?

You could do a simple while loop:

import subprocess

repeat = 'y'
while repeat == 'y':
    subprocess.call(["programname"])
    repeat = raw_input("Would you like to run again? (y/n) ")

Thanks for the quick answer i will try it out

Hmm it didnt work, plainy how do you make a script run more then once without manual help? How do you create a funkction that ask if you want to run the script once or forever. in the start, i have tested some functions but none seems to work.

double

class Repeat(Thread):
    
    y =raw_input("You want to run again? (y/n) ")
    if y == y:
        execfile("blah.py")
    elif y <> y:
        print "OK, then we exit"

somehow the y part work but if you dont choose y and press "n" the program should exit or stop but it doesnt.

y will always be equal to y. I think you meant if y == 'y' . Like wise for the not equals (note you can also use != ). You really don't need to have another clause there, because if y is not equal to the character 'y' you already know it's not equal so you don't need to specifically check again. You can simply use if y == 'y' and else .

Finally, you made a class. That's not the way you're supposed to use a class, you should have defined a function.

Thanks for reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.