今天在学Python的关键字“and,or”。
写了这个代码:
color = input("please enter your color :")
if color =="red" or color == "yellow" or color == "black" and color == "white":
print("haha man, you got it !")
else:
print("ennnnnn, sorry.")
却怎么也打不出 if 的结果,
试过了:"redwhite","red white", "red,white"
代码的运行结果都是:“ennnnnn, sorry.”
请问要怎么才能得到:“haha man, you got it !”?
谢谢!!!
你如果要判断同时包含red,和 white应该是
if ("red" in color or "yellow" in color or "black" in color) and "white" in color:
if color =="red" or color == "yellow" or color == "black" or color == "white":
这里不适合用and,条件永远不符合
if color =="red" or color == "yellow" or color == "black" or color == "white":
print("haha man, you got it !")
else:
print("ennnnnn, sorry.")
你的输入有问题的,那是颜色组合,输入了"redwhite","red white", "red,white",这些均使得if 的条件不成立,输入"black"试试,就行了
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632