Python:while(True!= True)loop
我开始学习本周的代码,所以我正在玩弄我创建的小程序,试图更好地理解它的工作原理。Python:while(True!= True)loop
我做的一个程序是一个Pig拉丁翻译器,循环直到用户退出。该程序的作品,但逻辑没有任何意义给我。
pyg = "ay" #Pig Latin words end with ay.
def translate(): #Creating a function.
original = input("Enter a word: ").lower() #Ask for input then convert to lower.
if len(original) > 0 and original.isalpha() : #isalpha() verifies only abc's and more than one letter.
first = original[0] #Assigns the first letter of the string to first.
latin = original[1:] + first + pyg #Adds original starting at 2nd letter with first and pyg.
print(latin)
else:
print("You did not enter a valid word, please try again.")
translate() #If you did not enter valid word, then call function again until you do.
translate() #Don't forget to actually call the function after you define it.
#Set run to False.
#Can be set to True if while (run != True) is set to while (run == True).
run = False
#Defining cont(). Ask for imput and error handling.
def cont():
loop = input("Would you like to convert another word? (y/n): ").lower()
if loop == "y" :
run = True
elif loop == "n" :
run = False
print("Thank you for using this program, have a nice day!")
exit()
else :
print("You did not enter a valid response, please try again.")
cont()
cont()
#Infinite loop as long as run is not equal to True.
while (run != True) :
translate()
cont()
我的问题是,为什么这个程序工作?我将运行设置为False,并将循环设置为只要运行!= True即可运行。那里没问题,但是当我定义cont()时,如果用户输入“y”,我将运行设置为True。 True!= True应该是False(如果我理解正确)并且循环应该结束,但是它会按照我的意愿工作。
这是我犯的一个编码错误还是我只是想着这个错误的方式?先谢谢你。
编辑:非常感谢大家回答。我还没有了解当地和全球的变数。
为了扩大在别人已经说过,在这些线路上run
if loop == "y" :
run = True
elif loop == "n" :
run = False
并不是指通过
#Can be set to True if while (run != True) is set to while (run == True).
run = False
run
在cont
函数的定义相同run
是一个局部变量你的函数,而不是全局定义的run
。
有一些(至少)方法来解决这个问题。首选(imo)的方式是让cont
返回一个新的值,分配给run
。这看起来就像
#Defining cont(). Ask for imput and error handling.
def cont(_run):
loop = input("Would you like to convert another word? (y/n): ").lower()
if loop == "y" :
return _run
elif loop == "n" :
return not _run
else :
print("You did not enter a valid response, please try again.")
return cont(_run)
...
#Infinite loop as long as run is not equal to True.
while (run != True) :
translate()
run = cont(run)
其他(不太优选)的方法是使用你的cont
函数内全球run
变量。这是使用global
关键字实现的。
这将是这样的:
#Defining cont(). Ask for imput and error handling.
def cont():
global run
loop = input("Would you like to convert another word? (y/n): ").lower()
if loop == "y" :
run = True
elif loop == "n" :
run = False
print("Thank you for using this program, have a nice day!")
exit()
else :
print("You did not enter a valid response, please try again.")
cont()
**夫妇旁注
在我的第一个例子中,我回到_run
当值y
和not _run
当值n
。这使您可以将初始值run
的值更改为True,并更改while
的条件,而无需更改cont
函数本身。
如果您使用全局,并且用户输入n
(因为您在函数返回之前退出),您根本不需要实际更改run
的值。
,因为很多人不读完整说明:)
我想这可能是因为你的运行变量的范围;因为你没有从你的cont函数返回运行。我相信你的!= True检查看到的总是在该函数之外是False,但显然你可以在函数内成功结束程序。
run
里面的cont
函数是一个局部变量。改变它的值对while循环引用的全局变量没有影响。
的问题,您可能会更好改变你if
条件检查
if loop in ("yes", "y"):
if loop in ("no", "n"):
的是,在cont()
定义的run
变量不与全局范围中定义的变量run
相同。 (如果你不确定你的意思,你可能想看看https://docs.python.org/3.4/tutorial/classes.html#python-scopes-and-namespaces。也许对你的代码更好的方法是让cont()
返回True
或False
。当你使用True
时它也更直观和更易读想继续下面是我将如何重写它
pyg = "ay" #Pig Latin words end with ay.
def translate(): #Creating a function.
original = input("Enter a word: ").lower() #Ask for input then convert to lower.
if len(original) > 0 and original.isalpha() : #isalpha() verifies only abc's and more than one letter.
first = original[0] #Assigns the first letter of the string to first.
latin = original[1:] + first + pyg #Adds original starting at 2nd letter with first and pyg.
print(latin)
else:
print("You did not enter a valid word, please try again.")
translate() #If you did not enter valid word, then call function again until you do.
#Defining cont(). Ask for imput and error handling.
def cont():
while True:
loop = input("Would you like to convert another word? (y/n): ").lower()
if loop == "y":
return True
elif loop == "n":
print("Thank you for using this program, have a nice day!")
return False
else :
print("You did not enter a valid response, please try again.")
translate()
while cont():
translate()