python开发一个菜单点菜功能,可以输入菜名前的数字编号点菜

  本例开发一个菜谱点菜功能,可以输入菜名前的数字编号点菜。 

import re
option_str = input("请输入数字或方向键:")
#re.sub(pattern,repl,string,count=0,flags=0)找到string中可以匹配pattern的部分,
#然后将这些部分替换成repl,本例就是将非数字的部分都去掉。
option = re.sub("\D","",option_str)
if int(option) == 1:
    print("选择数字1键执行的菜单")
if int(option) == 2:
    print("选择数字2键执行的菜单")
if int(option) == 3:
    print("选择数字3键执行的菜单")
if int(option) == 4:
    print("选择数字4键执行的菜单")
if int(option) == 5:
    print("选择数字5键执行的菜单")
if int(option) == 6:
    print("选择数字6键执行的菜单")
if int(option) == 7:
    print("选择数字7键执行的菜单")
if int(option) == 8:
    print("选择数字8键执行的菜单")
if int(option) == 0:
    print("退出系统")

结果:

请输入数字或方向键:5
选择数字5键执行的菜单

 

posted @ 2020-05-03 11:38  夏日的向日葵  阅读(1812)  评论(0编辑  收藏  举报