首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python: while (True != True)循环

Python: while (True != True)循环
EN

Stack Overflow用户
提问于 2015-08-14 20:53:01
回答 4查看 1.3K关注 0票数 0

这周我开始学习代码,所以我在玩我正在创建的小程序,试图更好地理解它是如何工作的。

我制作的程序之一是一个Pig拉丁语翻译器,它循环到用户退出。这个程序可以工作,但是逻辑对我来说没有任何意义。

代码语言:javascript
复制
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()

我的问题是,为什么这个程序有效?我将run设置为False,并将循环设置为运行时间为run != True。这里没有问题,但是当我定义cont()时,如果用户输入"y“,我将run设置为接受True值。True != True应该是False (如果我正确理解的话),循环应该结束,但是它正在按我的要求工作。

这是我犯的编码错误,还是我想错了?提前谢谢你。

编辑:非常感谢大家的回答。我还没有了解局部变量和全局变量。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-08-14 21:06:12

为了扩展其他人已经说过的内容,run在以下几行

代码语言:javascript
复制
if loop == "y" :
    run = True
elif loop == "n" :
    run = False

定义的相同的run

代码语言:javascript
复制
#Can be set to True if while (run != True) is set to while (run == True).
run = False

run函数中的cont是函数的局部变量,而不是全局定义的run

有几种方法(至少)可以解决这个问题。首选的(imo)方法是让cont返回一个分配给run的新值。看起来就像

代码语言:javascript
复制
#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)

另一种(不太喜欢的)方法是在您的run函数中使用全局cont变量。这是使用global关键字实现的。

看起来是这样的:

代码语言:javascript
复制
#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()

**附带说明

在我的第一个示例中,当值为y时,返回not _run;当值为n时,返回not _run。这允许您将初始run值更改为True,并更改while条件,而不必更改cont函数本身。

如果使用全局,并且用户输入run,则根本不需要实际更改n值,因为在函数返回之前退出。

如果将if条件检查更改为

代码语言:javascript
复制
if loop in ("yes", "y"):
if loop in ("no", "n"):

因为很多人没有阅读完整的说明:)

票数 1
EN

Stack Overflow用户

发布于 2015-08-14 21:00:37

run函数中的cont是一个局部变量。更改其值对while循环引用的全局变量没有任何影响。

票数 1
EN

Stack Overflow用户

发布于 2015-08-14 21:00:28

我认为这可能是因为您的run变量的范围;因为您没有从cont函数返回run。我相信在函数之外,您的!= True check所看到的始终是假的,尽管很明显,您可以在函数中成功地结束程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32018480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档