我完成了所有作业,但我忘记在子菜单下放置取消选项。我不想更改我的所有文件,这需要2天时间。在sub_menu.py
上添加取消并返回主菜单选项的最简单方法是什么?这是sub_menu.py
#!/usr/bin/python
## sub_menu.py
#=========#=========#=========#=========#=========#=========#=========#
def how_many():
while True:
print
print " How many do you want to draw? -----Range is from 1 to 5-----"
shape_num = raw_input(' Enter your choice: ')
try:
shape_num = int(shape_num)
if (1 <= shape_num <= 5):
print " Number ", shape_num ," is OK."
break
else:
print
print " The number you entered must be from 1 to 5 only."
except:
print
print " Only numbers allowed - Please try again"
return shape_num
#=========#=========#=========#=========#=========#=========#=========#
def main():
how_many()
#=========#=========#=========#=========#=========#=========#=========#
if __name__ == "__main__":
main()
#=========#=========#=========#=========#=========#=========#=========#
这是主要的
#!/usr/bin/python
## my_menu.py
import sub_menu as s_m
import draw_shape as d_s
#=========#=========#=========#=========#=========#=========#=========#
def main():
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 = s_m.how_many()
# draw in the middle of screen if == 1
if (my_shape_num == 1):
d_s.start_point(0, 0)
else:
d_s.start_point()
#
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)
d_s.t.end_fill() # shape fill color --draw_shape.py-- def start_point
else:
print
print ' Try again'
print
#=========#=========#=========#=========#=========#=========#=========#
if __name__ == "__main__":
main()
#=========#=========#=========#=========#=========#=========#=========#
这是drawshape的
#!/usr/bin/python
## draw_shape.py
import turtle as t
#=========#=========#=========#=========#
def start_point(x=-240, y=0):
# Added 23 dec 11 baris
t.color("black", "red")
t.shapesize(outline=8)
t.begin_fill()
#
t.penup()
t.setpos(x, y)
t.pendown()
def spaces_after(x=120):
t.penup()
t.forward(x)
t.pendown()
#=========#=========#=========#=========#
def draw_triangle(num=1):
for a in range(num):
for a in range(3):
t.forward(80)
t.left(120)
spaces_after()
#=========#=========#=========#=========#
def draw_square(num=1):
for a in range(num):
for a in range(4):
t.forward(80)
t.left(90)
spaces_after()
#=========#=========#=========#=========#
def draw_rectangle(num=1):
for a in range(num):
for a in range(2):
t.forward(80)
t.left(90)
t.forward(40)
t.left(90)
spaces_after()
#=========#=========#=========#=========#
def draw_pentagon(num=1):
for a in range(num):
for a in range(5):
t.forward(50)
t.left(72)
spaces_after()
#=========#=========#=========#=========#
def draw_hexagon(num=1):
for a in range(num):
for a in range(6):
t.forward(50)
t.left(60)
spaces_after()
#=========#=========#=========#=========#
def draw_octagon(num=1):
for a in range(num):
for a in range(8):
t.forward(40)
t.left(45)
spaces_after()
#=========#=========#=========#=========#
def draw_circle(num=1):
for a in range(num):
t . circle(50)
spaces_after()
#=========#=========#=========#=========#=========#=========#=========#
def main():
start_point()
draw_triangle()
draw_square()
draw_rectangle()
draw_pentagon()
draw_hexagon()
draw_octagon()
draw_circle()
t.end_fill()
#=========#=========#=========#=========#=========#=========#=========#
if __name__ == "__main__":
main()
#=========#=========#=========#=========#=========#=========#=========#
答案 0 :(得分:0)
你可以用两行代码完成。
在sub_menu.py
中你应该像这样添加“0”处理
...
try:
shape_num = int(shape_num)
if ( shape_num == 0):
return None
if (1 <= shape_num <= 5):
print " Number ", shape_num ," is OK."
break
else:
print
print " The number you entered must be from 1 to 5 only."
except:
....
并在main.py中你应该做
my_shape_num = s_m.how_many()
if ( my_shape_num is None):
continue
这应该重新打印主菜单。
如果需要,您还应该添加一条指令(按0取消)。