为什么这个while循环不起作用? [英] Why does this while loop not work?

查看:63
本文介绍了为什么这个while循环不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,即使我输入"no",并且输入"jdlfjap",该循环仍会继续循环.

This loop keeps looping even if I enter "no" and when I type "jdlfjap", for example, it continues to loop without a "?".

有人知道这是为什么吗?

Does anyone know why this is?

def makeContact():
    contactName = input("Name: ")
    contactNumber = input("Number: ")
    dictionaryForContacts[contactName] = contactNumber
def continueMaking():
    while True:
        continueMaking = input("\nWould you like to continue making contacts? ")
        if continueMaking == "Yes" or "yes" or "YES":
            makeContact()
            continue
        elif continueMaking == "No" or "no" or "NO":
            break
        else:    
            print ("?")
            continue

推荐答案

语句 if continueMaking =="Yes"或"yes"或"YES":等效于(continueMaking=="Yes")或"yes"或"YES":,无论 continueMaking 的值如何,它都会返回字符串"YES" ,即真实,因此 makeContact 调用始终会执行.不区分大小写的字符串比较可以通过 continueMaking.lower()==是" 来完成.

The statement if continueMaking == "Yes" or "yes" or "YES": is equivalent to (continueMaking == "Yes") or "yes" or "YES": which, regardless of the value of continueMaking returns the string "YES", which is truthy and thus the makeContact call always executes. Case-insensitive string comparisons can be accomplished by continueMaking.lower() == "yes".

这篇关于为什么这个while循环不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆