0

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?

6
  • 3
    while exit != exit:; What do you think this is doing? Commented Feb 27, 2015 at 5:41
  • while exit != exit: => should be while exit != "exit": Commented Feb 27, 2015 at 5:42
  • exit is equal to exit and hence exit!=exit means false. Commented Feb 27, 2015 at 5:42
  • Autoindent: Highlight the code block and press Ctrl-K. Check out How do I format my posts using Markdown or HTML? Commented Feb 27, 2015 at 5:43
  • ack.. that's what it was. forgot fkn quotes. Commented Feb 27, 2015 at 5:44

2 Answers 2

3
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.

Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.