python作业示例

You will write a personal recipe manager. Your program will let the user add information
about his/her favorite food recipes to the system and use commands to edit, sort or search
the information. Your program should be a function named MyFavRecipes which takes no
arguments and returns no values.
The recipe manager should be capable of accepting commands. Your function should print
out a prompt to indicate that the user can enter a command. Your prompt should be a ‘$’
character. At the prompt the user will type a command followed by a set of arguments for the
command. Your function will perform whatever action is indicated by the command, and then
your program will print a ‘$’ character on a new line in order to let the user know that he/she
can type a new command. This process will repeat until the user enters the “Exit” command.
Your program should accept the following commands.
1. AddRecipe: This command indicates that the user wants to enter the information about
a recipe into the system. It takes four arguments: the name of the recipe, cooking time (in
minutes : 0-100) , level of difficulty (1 the easiest and 5 the hardest), and the key ingredient
(e.g. milk and sugar), separated by commas. This command adds the information about the
new recipe into the system.
2. PrintRecipe: This command prints all the information about the recipes which have been
added to the system. It takes no input arguments. The information about each recipe is
printed on a separate line. Each line should have four pieces of information on it: the name
of the recipe, cooking time, level of difficulty, and the key ingredient, separated by commas.
3. DeleteRecipe: This command indicates that the user wants to delete the information
about a recipe from the system. This command should be followed by one argument, the
name of the recipe.
4. SortRecipe: This command indicates that the user wants to view a sorted list of recipes.
This command should be followed by one argument, either “ Time ” or “ Difficulty ”. It will
print the information about the recipes which have been entered into the system, sorted by
their cooking time or level of difficulty indicated by the input argument. The information
about each recipe is printed on a separate line. Each line should have four pieces of
information on it: the name of the recipe, cooking time, level of difficulty, and the key
ingredient, separated by commas. Keep in mind that sorting should be listed in descending order. In case of ties, the ordering
is arbitrary.
5. FindByTime: This command searches for all recipes with cooking time below a given
threshold (0 to 100). It should be followed by one argument, cooking time threshold. For
each recipe in the system with a cooking time below the threshold,the name of the recipe
should be printed on a single line.
6. FindByName: This command searches the system for a recipe with a certain name. The
command should be followed by one argument, the name of the recipe. If a recipe with the
given name is in the system, all the information about it should be printed on a single line:
the name of the recipe, cooking time, level of difficulty, and the key ingredient, separated by
commas.
7. FindByIngredient: This command searches the system for a recipe with a certain
ingredient. The command should be followed by one argument, the ingredient. If a recipe
with the given ingredient exists in the database, all the information about it should be
printed on a single line: the name of the recipe, cooking time, level of difficulty, and the key
ingredient, separated by commas.
8. Exit: This command should end the program
#Q:491516541
#V:examall
def printr(r):
    '''print one recipe'''
    print(','.join([r.name,r.time,r.level,]),','.join(r.ingredient),sep=',')
def AddRecipe(s):
    '''add a recipe
    '''
    lst=s.split(',')
    if len(lst)<4:return 'param error'
    tmp=lst[3:];lst=lst[:3];lst.append(tmp)
    r=Recipt(*lst)
    try:
        n=int(r.time)
        if n<0 or n>100:return 'time error'
    except :
        return 'time error'
    try:
        n=int(r.level)
        if n<1 or n>5:return 'level error'
    except :
        return 'level error'
    ListOfRecipes.append(r)
def PrintRecipe(s):
    '''print all recipes
    '''
    for r in ListOfRecipes:printr(r)
def DeleteRecipe(s):
    ''' del one recipt by name 
    '''
    for i,r in enumerate(ListOfRecipes):
        if r.name==s:
          del ListOfRecipes[i]
def SortRecipe(s):
    ''' sort recipes by Difficulty(from hardest to easiest) or Time
    '''
    if s=='Difficulty':
        lst=sorted(ListOfRecipes,key=lambda x: x.level,reverse=True)
    elif  s=='Time':
        lst=sorted(ListOfRecipes,key=lambda x: x.time,reverse=True)
    else :
        return 'sort key error'
    for r in lst:printr(r)
def FindByTime(s):
    ''' find recipes which time below the given param
    '''
    try:
        t=int(s)
    except :
        print('time error')
    for i,r in enumerate(ListOfRecipes):
        if int(r.time)<t:
            printr(r)
def FindByName(s):
    '''find a recipe by name
    '''
    for i,r in enumerate(ListOfRecipes):
        if r.name==s:
            printr(r)
def FindByIngredient(s):
    '''find  recipes by ingredient
    '''
    for i,r in enumerate(ListOfRecipes):
        if s in r.ingredient:
            printr(r)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值