2012-01-22 195 views
2

我是新来的Python,我不知道为什么,但下面的代码中的ifelif不能像我期望的那样工作。然而,if,elif not working as expected expected

  • 它完美,当我键入1至7

  • 它完美的作品,当我键入0 8或9(它说: “重试”)

  • 它不工作如果我输入10到69,100到任何数量的

当我说这是行不通的我的意思是它打印

my_shape_num = h_m.how_many() 

但我不知道为什么。它必须停止,如果选择的是编辑没有和7


之间1

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 

回答

9

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

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

除了接受'7',这将接受123456开头的字符串。

要解决此问题,只要您测试了'x',就立即将choice转换为整数,然后使用整数比较。

+0

谢谢。我无法弄清楚如何处理其他不是数字的输入。 – emre

+0

我已经做到了这一点:'选择的raw_input =( '请输入您的选择:') 如果(选择== 'X')或(选择== 'X'): 突破 尝试: 选择= INT (选择) 如果(1 <=选择<= 7): 如果选择== 1: d_s.draw_triangle(my_shape_num) 否则: 打印 打印 '!数量必须是从1到7' 打印 除了ValueError异常: 打印 打印“再试一次” print' – emre

+0

@ baris22:很难读粘贴到注释中的代码,但你是正确的轨道上:我想尝试'INT(选择) '赶上'ValueError'是要走的路。 – NPE

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

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

然后,当然,你还需要人打字事情是不是数字有所准备:

int('hi')  # ValueError 
2

我想这是因为你使用的字符串比较...尝试

choice = int(choice) 

之前如果ELIF块和改变自己的比较,

if choice == 1: 

(不带引号)

相关问题