Black Sheep
Black Sheep

Reputation: 21

why does this python program automatically close?

this program quits after printing the first line and seemingly doesn't enter the while loop. i'm at a dead end here, any help would be appreciated. it's design is to create files with the same name at different directories

print ("What is the name of the command to create?: ")
#loops until program is done
exit = "placeholder"
while exit != exit:
#filename to use for directory creation
    cmd = input()
#combines cmd name with directory
    cmdDir = "/usr/bin/" + cmd
#makes sure something was entered as a cmd name
    if cmdDir == "/usr/bin/":
        print ("Command name invalid. Try again: ")

    else: 
#creates file at directory with cmd name
        open (cmdDir, 'a')
            print ("Will this command have a python extension?: ")
#loops until program is done
            while exit != exit:
                decision = input()
#combines cmd name with python directory
                pythonDir = "/root/python/" + cmd + ".py"
                if decision == "yes":
#creates directory              
                    open (pythonDir, 'a')
                    print ("Command directories " + cmdDir + " and" + pythonDir + " created. ")
#sets program to exit while loops
                    exit = "exit"
                elif decision == "no":
                    print ("Command directory " + cmdDir + "created. ")
#sets program to exit while loops
                    exit = "exit"
                else:
                    print ("Enter yes or no: ")

ps: formatting this manually with the 4-space indent was a pain in the ass, how does the auto-indent work?

Upvotes: 0

Views: 69

Answers (2)

Jaimin Nimavat
Jaimin Nimavat

Reputation: 91

while exit != exit:

is what is messing you up. Since they are the same variable exit will always equal exit. Since exit is a string there are no ways around this.

So the code you might wanna use could be while exit != 'exit': This will stop the program from exiting on line 1. The reason for this is that if the variable exit doesn't have the value of 'exit' then the program will run.

Hope this helps.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882366

while exit != exit:

exit will always be equal to exit, since they're the same variable (there are exceptions to this rule in certain circumstances but not for a string type in Python). So that expression will always be false and you'll never enter the body of the loop.

You probably meant to do this instead:

while exit != 'exit':

which compares the variable with a fixed string constant.

Upvotes: 3

Related Questions