How to handle both integer and string from raw_input?

前端 未结 5 415
一向
一向 2021-01-14 12:56

Still trying to understand python. It is so different than php.

I set the choice to integer, the problem is on my menu I need to use letters as well.

How c

5条回答
  •  梦谈多话
    2021-01-14 13:32

    I believe your code can be simplified a bit:

    def main():
        while 1:
            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 "  D - Display what was drawn"
            print "  X - Exit"
            print
            choice = raw_input('  Enter your choice: ').strip().upper()
            if choice == 'X':
                break
            elif choice == 'D':
                log.show_log()
            elif choice in ('1', '2', '3', '4', '5', '6', '7'):
                my_shape_num = h_m.how_many()
                if my_shape_num is None:
                    continue
                elif 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()
            else:
                print
                print '  Invalid choice: ' + choice
                print
    

提交回复
热议问题