即使我输入“no”,这个循环也会保持循环,例如,当我键入“jdlfjap”时,它会继续循环而没有“?”。
有谁知道这是为什么?
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
答案 0 :(得分:2)
语句if continueMaking == "Yes" or "yes" or "YES":
相当于(continueMaking == "Yes") or "yes" or "YES":
,无论continueMaking
的值如何,都会返回字符串"YES"
,这是真实的,因此makeContact
呼叫始终执行。不区分大小写的字符串比较可以通过continueMaking.lower() == "yes"
完成。
答案 1 :(得分:1)
用变量continueMaking
覆盖函数continueMaking
会增加混乱。选择其他变量名称。可读性很重要。