0
"""
This program presents a menu to the user and based upon the selection made
invokes already existing programs respectively.
"""
import sys

def get_numbers():
  """get the upper limit of numbers the user wishes to input"""
  limit = int(raw_input('Enter the upper limit: '))
  numbers = []

  # obtain the numbers from user and add them to list
  counter = 1
  while counter <= limit:
    numbers.append(int(raw_input('Enter number %d: ' % (counter))))
    counter += 1

  return numbers

def main():
  continue_loop = True
  while continue_loop:
    # display a menu for the user to choose
    print('1.Sum of numbers')
    print('2.Get average of numbers')
    print('X-quit')

    choice = raw_input('Choose between the following options:')

    # if choice made is to quit the application then do the same
    if choice == 'x' or 'X':
      continue_loop = False
      sys.exit(0)

    """elif choice == '1':  
         # invoke module to perform 'sum' and display it
         numbers = get_numbers()
         continue_loop = False
         print 'Ready to perform sum!'

       elif choice == '2':  
         # invoke module to perform 'average' and display it
         numbers = get_numbers()
         continue_loop = False
         print 'Ready to perform average!'"""

     else:
       continue_loop = False    
       print 'Invalid choice!'  

if __name__ == '__main__':
  main()

My program processes only if I enter 'x' or 'X' as input. For other inputs the program just quits. I've commented out the elif parts and ran with only if and else clauses. Now a syntax error is thrown. What am I doing wrong?

1
  • Your syntax error is coming from the else: line being too indented by one space. Commented Jan 5, 2013 at 15:15

4 Answers 4

3

It's about the line if choice == 'x' or 'X'.

Correctly, it should be

if choice == 'x' or choice == 'X'

or simpler

if choice in ('X', 'x')

because the or operator expects boolean expressions on both sides.

The current solution is interpreted as follows:

if (choice == 'x') or ('X')

and you can clearly see that 'X' does not return a boolean value.

Another solution would be of course to check whether if the uppercase letter equals 'X' or the lowercase letter equals 'x', which might look like that:

if choice.lower() == 'x':
    ...
Sign up to request clarification or add additional context in comments.

4 Comments

'X' can be used in an if statement - but as it is a non-empty string it will evaluate to True and cause the statement if choice =='x' or 'X' to always be True.
Good observation, of course you are right, but I just simplified it to show him that it makes no sense in this context to evaluate a non-empty string.
Never know that or expects boolean on both sides. Thanks for explaining it more clearly as well as more pythonic.
No problem. As @DanielB explained before, 'or' doesn't strictly expect booleans on both sides, e.g. non-empty strings evaluate to 'True' too. I just wanted to show that this would be semantically false in that context.
2

Your problem is with your if choice == 'x' or 'X': part.To fix that change it to this:

if choice.lower() == 'x':

Comments

0
if choice == 'x' or 'X':

is not doing what you think it's doing. What actually get's parsed is the following:

if (choice == 'x') or ('X'):

You probably want the following:

if choice == 'x' or choice == 'X':

which can be written as

if choice in ('x', 'X'):

3 Comments

that can be written as if choice in ('x', 'X')
or in this case, the even simpler if choice.lower() == 'x':
@bgporter. This is a more language independent choice of writing the expression. Thanks a lot for reminding.
0

As the interpreter says, it is an IndentationError. The if statement on line 31 is indent by 4 spaces, while the corresponding else statement is indent by 5 spaces.

1 Comment

Thanks, since I copied code and pasted while asking question, I had to intend them and by mistake I would've added an additional space

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.