该程序在打印完第一行后退出,似乎没有进入while循环。我在这里走到了尽头,任何帮助都会受到赞赏。它的设计是在不同的目录中创建具有相同名称的文件
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:使用4空格缩进手动格式化这是一个痛苦的屁股,自动缩进是如何工作的?
答案 0 :(得分:3)
while exit != exit:
exit
总是等于exit
,因为它们是相同的变量(在某些情况下此规则有例外但对字符串不存在输入Python)。所以表达式总是假的,你永远不会进入循环体。
您可能打算这样做:
while exit != 'exit':
将变量与固定字符串常量进行比较。
答案 1 :(得分:0)
while exit != exit:
是什么让你搞砸了。由于它们是相同的变量exit
将始终等于exit
。由于exit
是一个字符串,因此无法解决此问题。
所以您可能希望使用的代码可以是while exit != 'exit':
这将阻止程序退出第1行。原因是如果变量exit
没有值'exit'
然后程序将运行。
希望这有帮助。