如果,elif没有按预期工作

时间:2012-01-22 11:38:49

标签: python

我是Python新手,我不知道为什么,但以下代码中的ifelif无法正常工作。然而,

  • 我输入1到7

  • 时效果很好
  • 当我输入0 8或9(它说“再试一次”)时,它的效果非常好

  • 如果我输入10到69,100到任意数字

  • ,它就不起作用

当我说它不起作用时,我的意思是打印

my_shape_num = h_m.how_many()

但我不知道为什么。如果选择不在1到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

编辑:好的,排序:

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

3 个答案:

答案 0 :(得分:9)

比较字符串lexicographically'10'大于'1'但小于'7'。现在考虑这段代码:

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

除了接受'7'之外,这还会接受以12345开头的任何字符串或6

要修复,请在测试choice后立即将'x'转换为整数,然后再使用整数比较。

答案 1 :(得分:3)

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

您正在比较字符串(文本),因此顺序就像字典。您需要将它们转换为整数(数字),以便比较将它们按计数顺序排列。

然后,当然,您还需要为人们输入非数字的东西做好准备:

int('hi')       # ValueError

答案 2 :(得分:2)

我认为这是因为您使用字符串进行比较...尝试

choice = int(choice)

之前if,elif阻止并将其比较改为

if choice == 1:

(不含引号)