为什么`letter ==" A"或“a”始终评估为真? [英] Why does `letter=="A" or "a"` always evaluate to True?
本文介绍了为什么`letter ==" A"或“a”始终评估为真?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请查看代码。我正在使用机器人汽车来画一个字母,在这个代码中,当我输入b时,它仍然会画小案例a。
Please look at the code. I'm using a robot car to draw a letter and in this code, when I type b, it will still draw small case a.
import create
# Draw a:
def drawa():
#create robot
robot = create.Create(4)
#switch robot to full mode
robot.toFullMode()
for i in range(1280):
robot.go(20,30)
robot.stop()
robot.move(-40,20)
# Draw b:
def drawb():
#create robot
robot = create.Create(4)
#switch robot to full mode
robot.toFullMode()
robot.move(-100,20)
for i in range(1270):
robot.go(20,-30)
robot.stop()
# Draw c:
def drawc():
#create robot
robot = create.Create(4)
#switch robot to full mode
robot.toFullMode()
for i in range(700):
robot.go(20,30)
robot.stop()
# Define Main Function
def main():
# While loop
while(True):
# Prompt user to enter a letter
letter = raw_input("Please enter the letter you want to draw: ")
# If user enters the letter a, draw a
if letter=="A" or "a":
drawa()
# If user enters the letter b, draw b
elif letter=="B" or "b":
drawb();
# If user enters the letter c, draw c
elif letter=="C" or "c":
drawc();
# If user enters anything other than a letter from a-z,
# ask them to enter a valid input
else:
print("Please enter a letter from a-z.")
main()
请帮忙。
推荐答案
这是因为你的条件。当你说...
It's because of your conditions. When you say...
if letter == "A" or "a"
...你实际上在说......
...you are actually saying...
if it's true that 'letter' equals 'A', or is true that 'a'
...和a
,作为非空字符串,总是计算为true。您不是在或
右侧的 letter
中询问任何内容。这样做:
... and "a"
, as a non-empty string, evaluates always to true. You are not asking anything from letter
in the right-hand side of the or
. Do this:
if letter == "A" or letter == "a"
或者,因为我们在python中:
Or, since we're in python:
if letter in ["A", "a"]
干杯!
这篇关于为什么`letter ==" A"或“a”始终评估为真?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文