The Big IF - Else statement and other issues


#1

Hi.

using the code below, i am able to get the results i want when entering a number ( for the if statement and both elif statements).
But when i enter in a non numeric answer, instead of getting the “else” response, i get the following:

Traceback (most recent call last):
_ File “python”, line 23, in _
_ File “python”, line 3, in the_flying_circus_
ValueError: invalid literal for int() with base 10: ‘asd’

**# Make sure that the_flying_circus() returns True
def the_flying_circus():
** a = int(raw_input(“enter a number!”))
** if (a >= 18 and a <= 21):
** print “young, but now allowed to marry”
** # Start coding here!
** # Don’t forget to indent
** # the code inside this block!
** elif (a > 21):
** print “yeah, you can marry”
** # Keep going here.
** # You’ll want to add the else statement, too!
**
** elif (a < 18):
** print “Too young to marry”
** return True
**
** else:
** print “you did not enter in a number”
** return True


the_flying_circus()

I also tried the code below (with out the “int” in the a= statement. This was said to work according to one of the discussion replies. but in this code, even if i enter in 18, 21 or a number between them, the response jumps the “if” and returns the first “elif” statement.

def the_flying_circus():
** #indent 4 spaces
** a = raw_input("enter your age ")
** if (a >= 18 and a <= 21): # Start coding here!
** #indent 4 spaces, to indicate the IF code-block
** print “young, but not allowed to marry”
** return True
** # Don’t forget to indent
** # the code inside this block!
** elif (a >= 21):
** #indent 4 spaces, to indicate the ELIF code-block
** print “Yeah, you can marry”
** return True
** elif(a < 18):
** #indent 4 spaces, to indicate the ELIF code-block
** print “age is too low”
** return True
** # Keep going here.
** # You’ll want to add the else statement, too!
** else:
** #indent 4 spaces, to indicate the ELSE code-block
** print “something you entered which is not age”
** return True

the_flying_circus()

Please help me understand the reason for these outcomes and what i should be doing differently.

thanks.


#3

For “The Big If” lesson, you don’t need to get a raw input from the user. Keep it simple. Take a look at the instructions. It’s merely teaching you about control structure using the if/elif/else control.

Take a look at the hint too.

Dealing with Raw Input

Are you wanting to really understand raw_input and the error you’re receiving? You need to fetch the raw input from the viewer and then validate it to ensure it’s an integer. If no, bail out. If yes, then do the type casting with a = int(a).

Strategies include:

  1. Wrap it the conversion into a try.
  2. Or using a.isdigit().

Using the latter, you do:

def the_flying_circus():
  a = raw_input('enter a number!')

  # bail out if it's not a number
  if not a.isdigit():
      print 'you did not enter in a number'
      return False

  # okay, it's a number. Convert it into an integer
  a = int(a)

  # do the rest here.

But stick to the instructions for this exercise. Keep it simple.


#4

Thanks for that. I’ll give it a try and see what happens.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.