arr*_*ggg 1 python syntax if-statement or-operator
请看代码.我正在使用机器人汽车来画一个字母,在这个代码中,当我输入b时,它仍然会画一个小案例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()
Run Code Online (Sandbox Code Playgroud)
请帮忙.
这是因为你的条件.当你说...
if letter == "A" or "a"
Run Code Online (Sandbox Code Playgroud)
......你其实在说......
if it's true that 'letter' equals 'A', or is true that 'a'
Run Code Online (Sandbox Code Playgroud)
...并且"a",作为非空字符串,始终评估为true.你没有letter在右边那边问任何东西or.做这个:
if letter == "A" or letter == "a"
Run Code Online (Sandbox Code Playgroud)
或者,因为我们在python中:
if letter in ["A", "a"]
Run Code Online (Sandbox Code Playgroud)
干杯!