1

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.

arrgggg
  • 19
  • 1
  • 5
  • 6
    1-space indent is horrible to read. Please use 4-space indent in the future as suggested in [PEP-8](http://www.python.org/dev/peps/pep-0008/). – ThiefMaster Dec 05 '10 at 17:20
  • 1
    1 space is too small; 2 spaces are ideal; 3 are acceptable; 4 are orthodox; 5 were never used; 6 are gross; 7 are failure; 8 were in the beginning. i recommend to always use 2 spaces, and even proscribe that in python's syntax. it is part of the grammar of the language. i likewise doubt the utility of many other points raised in pep 8. – flow Dec 05 '10 at 19:26

6 Answers6

8

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!

salezica
  • 74,081
  • 25
  • 105
  • 166
  • 4
    A tuple (`letter in ('A', 'a')`) would actually be most idiomatic and slightly faster, but that's just nitpicking. I also like `x.strip().lower() == ...` when dealing with longer input, as it's very robust and short. –  Dec 05 '10 at 17:34
  • delnan, more than about being fast, it looks natural. But maybe that is just me! – user225312 Dec 05 '10 at 17:37
  • (if letter == "A" or letter == "a") actually worked. Thanks alot. :) – arrgggg Dec 05 '10 at 22:41
1

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.

Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

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()
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
0
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'
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
0
 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.

user225312
  • 126,773
  • 69
  • 172
  • 181
0

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()
Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301
  • this is really nice code. it cuts down all the extra codings...lol it works with all lowercase letters, but when i typed in uppercase letters, i get errors. i'll see if i can figure it out. thanks alot. :) – arrgggg Dec 05 '10 at 23:14
  • @arrgggg: Oops, sorry, fixed -- for uppercase letters it was trying to call `drawA()` instead of `drawa()` for example. – martineau Dec 06 '10 at 02:14