eMRe
eMRe

Reputation: 3247

if, elif not working as expected

I am new to Python and I do not know why but the if, elif in the following code is not working as I expect it to. However,

When I say it does not work I mean it prints

my_shape_num = h_m.how_many()

But I do not know why. It has to stop if choice is not between 1 and 7

def main(): # Display the main menu
    while True:
        print
        print "  Draw a Shape"
        print "  ============"
        print
        print "  1 - Draw a triangle"
        print "  2 - Draw a square"
        print "  3 - Draw a rectangle"
        print "  4 - Draw a pentagon"
        print "  5 - Draw a hexagon"
        print "  6 - Draw an octagon"
        print "  7 - Draw a circle"
        print
        print "  X - Exit"
        print

        choice = raw_input('  Enter your choice: ')

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

        elif (choice >= '1' and choice <= '7'):
            my_shape_num = h_m.how_many()
            if ( my_shape_num is None): 
                continue

            d_s.start_point() # start point on screen

            if choice == '1': 
                d_s.draw_triangle(my_shape_num) 
            elif choice == '2': 
                d_s.draw_square(my_shape_num) 
            elif choice == '3':             
                d_s.draw_rectangle(my_shape_num) 
            elif choice == '4':             
                d_s.draw_pentagon(my_shape_num) 
            elif choice == '5':             
                d_s.draw_hexagon(my_shape_num) 
            elif choice == '6':             
                d_s.draw_octagon(my_shape_num) 
            elif choice == '7': 
                d_s.draw_circle(my_shape_num)

        else:
            print
            print '  Try again'
            print

Edit: Ok, sorted:

choice = raw_input('  Enter your choice: ')

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


try:
    choice = int(choice)
    if (1 <= choice <= 7):

        my_shape_num = h_m.how_many()
        if ( my_shape_num is None): 
            continue

        d_s.start_point() # start point on screen

        if choice == 1: 
            d_s.draw_triangle(my_shape_num) 
        elif choice == 2: 
            d_s.draw_square(my_shape_num) 
        elif choice == 3:             
            d_s.draw_rectangle(my_shape_num) 
        elif choice == 4:             
            d_s.draw_pentagon(my_shape_num) 
        elif choice == 5:             
            d_s.draw_hexagon(my_shape_num) 
        elif choice == 6:             
            d_s.draw_octagon(my_shape_num) 
        elif choice == 7: 
            d_s.draw_circle(my_shape_num)

    else:
        print
        print '  Number must be from 1 to 7!'
        print

except ValueError:
    print
    print '  Try again'
    print

Upvotes: 2

Views: 2654

Answers (3)

Thomas K
Thomas K

Reputation: 40390

'43' < '7'           # True
43 < 7               # False
int('43') < int('7') # False

You're comparing strings (text), so the order is like a dictionary. You need to convert them into integers (numbers), so that comparisons put them in counting order.

Then, course, you also need to be prepared for people typing things that aren't numbers:

int('hi')       # ValueError

Upvotes: 3

WestFlame
WestFlame

Reputation: 435

I think it's because you are using string for comparing... try

choice = int(choice)

before if, elif block and change their comparisons to

if choice == 1:

(without quotes)

Upvotes: 2

NPE
NPE

Reputation: 500913

Strings are compared lexicographically: '10' is greater than '1' but less than '7'. Now consider this code:

elif (choice >= '1' and choice <= '7'):

In addition to accepting '7', this will accept any string beginning with 1, 2, 3, 4, 5 or 6.

To fix, convert choice to integer as soon as you've tested for 'x', and use integer comparisons thereafter.

Upvotes: 9

Related Questions