Why does `letter==“A” or “a”` always evaluate to True? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-11-30 09:44:13

问题


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()

please help.


回答1:


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'

... 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"

Or, since we're in python:

if letter in ["A", "a"]

Cheers!




回答2:


if letter=="A" or "a": is incorrect. Use if letter == "A" or letter == "a":

Your code evaluates to if yourcondition or True (a non-empty strng in a boolean context is true) which basically means if True.

Same applies to the other if conditions.




回答3:


You don't need semi-colons in Python.

Also, do letter = letter.lower() so that you can simplify your case to if letter = 'a':

This works for me -

# 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: ").lower()

  # If user enters the letter a, draw a
  if letter == "a":
    print "in A: %s" % letter
  # If user enters the letter b, draw b
  elif letter == "b":
    print "in B: %s" % letter
  # If user enters the letter c, draw c
  elif letter == "c":
    print "in C: %s" % letter
  # 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()



回答4:


letter == "B" or "b"

does not do what you think it does. It asks if letter is equal to "B" and, if not, it returns 'b'.

Do this instead:

letter.lower() == 'b'



回答5:


 if letter in ('A', 'a'):
  drawa()
 # If user enters the letter b, draw b
 elif letter in ('B', 'b'):
  drawb()

This is how you should write it, the reasons have been given. Note that it should preferably be a tuple ('A', 'a') and a not a list.




回答6:


The problem is with your if/elif statements -- for example the first letter=="A" or "a" logical expression is evaluated like this ((letter=="A") or ("a")) because of operator precedence and so will always evaluate to True even if the letter isn't equal to an "A" (the or "a" part is always True because "a" isn't an empty string). There are a number of ways to fix that -- the simplest probably being to just change the expressions to follow this pattern letter=="A" or letter=="a" which is evaluated like this ((letter=="A") or (letter=="a")).

You could simplify the if/elif/else logic considerably using the technique shown in my [somewhat controversial] answer to a similar question. Applying it to what you're doing might result in something like the following:

import create

# Draw a:
def drawa():
 ...

# Draw b:
def drawb():
 ...

# Draw c:
def drawc():
 ...

# etc,,,

# Define Main Function
def main():
 while True:
  # Prompt user to enter a letter
  letter = raw_input("Please enter the letter you want to draw: ")
  if len(letter) > 0: 
   letter = letter[0].lower() # convert to lowercase and remove any excess
  # If first letter of what user entered was in the proper range, draw it
  if 'a' <= letter <= 'z':
   globals()['draw'+letter]()
  else: # otherwise ask them to try again
   print("Please enter a letter from a-z.")

main()


来源:https://stackoverflow.com/questions/4360066/why-does-letter-a-or-a-always-evaluate-to-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!