【发布时间】:2012-01-22 14:07:20
【问题描述】:
仍在尝试理解 python。它与php是如此不同。
我将选项设置为整数,问题出在我的菜单上,我也需要使用字母。
如何同时使用整数和字符串?
为什么我不能设置为字符串而不是整数?
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 " D - Display what was drawn"
print " X - Exit"
print
choice = raw_input(' Enter your choice: ')
if (choice == 'x') or (choice == 'X'):
break
elif (choice == 'd') or (choice == 'D'):
log.show_log()
try:
choice = int(choice)
if (1 <= choice <= 7):
my_shape_num = h_m.how_many()
if ( my_shape_num is None):
continue
# draw in the middle of screen if there is 1 shape to draw
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 ' Number must be from 1 to 7!'
print
except ValueError:
print
print ' Try again'
print
【问题讨论】:
-
不清楚是什么问题;菜单方面似乎工作正常:输入 X 时执行 break,输入 D 时执行 log.show_log(),输入 1..7 中的整数时分配 my_shape_num。
-
问题是:如果选择是D;我得到 log.show_log() 然后我得到错误消息'再试一次'
-
所以问题与string/int无关;它是你的“尝试”应该被包裹在一个“其他”中。
标签: python user-input