我是新来的Python,我不知道为什么,但下面的代码中的if
,elif
不能像我期望的那样工作。然而,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
谢谢。我无法弄清楚如何处理其他不是数字的输入。 – emre
我已经做到了这一点:'选择的raw_input =( '请输入您的选择:') 如果(选择== 'X')或(选择== 'X'): 突破 尝试: 选择= INT (选择) 如果(1 <=选择<= 7): 如果选择== 1: d_s.draw_triangle(my_shape_num) 否则: 打印 打印 '!数量必须是从1到7' 打印 除了ValueError异常: 打印 打印“再试一次” print' – emre
@ baris22:很难读粘贴到注释中的代码,但你是正确的轨道上:我想尝试'INT(选择) '赶上'ValueError'是要走的路。 – NPE