开发者社区> 问答> 正文

将信息传递到菜单中的功能时遇到问题

这是我正在处理的菜单的一部分。我能够获得第一个选择(“教练”的第一选择);但是,我在其他所有选择上都有困难。当我在菜单中选择2时,出现此错误:

“ NameError:名称'初学者'未定义”

我非常有信心,我需要在代码的“ def beginner():”部分中添加一些内容,但是添加的内容似乎无效。

下面是我的整个代码。任何见解都将不胜感激!

import logging
import boto3
import pprint
from botocore.exceptions import ClientError

def mainMenu(): 
    print("1. Are you looking for a coach? Here is their conact data.")
    print("2. Are you a beginner? Select 2 for your exersises.")
    print("3. Are you some where in the middle of your journey? Select 3 for your exersises.")
    print("4. Are you more Advanced? Select 4 for your exersises.")
    print("5. Are you hardcore and love to workout? Select 5.")
    print("6. Look at progress photos here!")
    print("7. Quit")   

    selection=int(input("Lets lose weight together!!! Please Select a Number:"))
    if selection==1: 
       coach()
    elif selection==2:
         beginner()
    elif selection==3: 
        intermediate()
    elif selection==4: 
       advanced()
    elif selection==5: 
        hardcore()
    elif selection==6: 
        progress_Photos()
    elif selection==7: 
        exit()
    else:
        print("Invalid choice. Enter 1-7")
        while selection == 7:
            break
        mainMenu()
    selection=int(input("Lets lose weight together!!! Please Select a Number:"))
"""Selection number 1 Input user data into DynamoDB database """
def coach():
       dynamodb = boto3.resource("dynamodb")
       table = dynamodb.Table('coachdata')
       response = table.scan()
       pprint.pprint(response, depth=3, compact = True)
mainMenu()
"""Selection number 2 Calculate exersises for beginners """     
def beginner():
    poundsToLose = input("How many pounds would you like to lose this month? ")
    pushups = '2'
    situps = '10'
    plankseconds = '10'
    pushupexersises = poundsToLose * pushups 
    situpexersises = poundsToLose * situps
    plankexersises = poundsToLose * plankseconds
    answer = 
    return answer
    print ("To lose {} pound this month do {} pushups {} situps and {} planks every day.".format(poundsToLose,pushupexersises,situpexersises,plankexersises))
mainMenu()    
"""Selection number 3 Calculate exersises for intermediate users"""      
def intermediate():
    poundsToLose = input("How many pounds would you like to lose this month? ")
    pushups = '5'
    situps = '10'
    plankseconds = '10'
    pushupexersises = poundsToLose * pushups 
    situpexersises = poundsToLose * situps
    plankexersises = poundsToLose * plankseconds
    print ("To lose (poundsToLose) pound this month do (pushupexersises) pushups (situpexersises) situps and (plankexersises) planks every day.")
mainMenu()       
"""Selection number 4 Calculate exersises for advanced users"""
def advanced():
    poundsToLose = input("How many pounds would you like to lose this month? ")
    pushups = '10'
    situps = '20'
    plankseconds = '20'
    pushupexersises = poundsToLose * pushups 
    situpexersises = poundsToLose * situps
    plankexersises = poundsToLose * plankseconds
    print ("To lose (poundsToLose) pound this month do (pushupexersises) pushups (situpexersises) situps and (plankexersises) planks every day.")
mainMenu()      
"""Selection number 5 Calculate exersises for hardcore users"""   
def hardcore():
    poundsToLose = input("How many pounds would you like to lose this month? ")
    pushups = '20'
    situps = '30'
    plankseconds = '30'
    pushupexersises = poundsToLose * pushups 
    situpexersises = poundsToLose * situps
    plankexersises = poundsToLose * plankseconds
    print ("To lose (poundsToLose) pound this month do (pushupexersises) pushups (situpexersises) situps and (plankexersises) planks every day.")
mainMenu()       
"""Selection number upload progress photos to S3 """     
def progress_Photos(bucket_name, object_name):
    """Retrieve an object from an Amazon S3 bucket

    :param bucket_name: string
    :param object_name: string
    :return: botocore.response.StreamingBody object. If error, return None.
    """

    # Retrieve the object
    s3 = boto3.client('s3')
    try:
        response = s3.get_object(Bucket=bucket_name, Key=object_name)
    except ClientError as e:
        # AllAccessDisabled error == bucket or object not found
        logging.error(e)
        return None
    # Return an open StreamingBody object
    return response['Body']


def mainGet():
 """Exercise get_object()"""

    # Assign these values before running the program
test_bucket_name = 'edu.umuc.sdev400.rharner.letsworkout'
test_object_name = 'beforeandafter.PNG'

    # Set up logging
logging.basicConfig(level=logging.DEBUG,
                        format='%(levelname)s: %(asctime)s: %(message)s')

    # Retrieve the object
stream = progress_Photos(test_bucket_name, test_object_name)
if stream is not None:
        # Read first chunk of the object's contents into memory as bytes
        data = stream.read(amt=1024)

        # Output object's beginning characters
        logging.info(f'{test_object_name}: {data[:60]}...')


if __name__ == '__main__':
    mainMenu()

问题来源:stackoverflow

展开
收起
is大龙 2020-03-25 00:21:20 553 0
1 条回答
写回答
取消 提交回答
  • 您可以在每个函数定义下调用mainMenu

    """Selection number 1 Input user data into DynamoDB database """
    def coach():
           dynamodb = boto3.resource("dynamodb")
           table = dynamodb.Table('coachdata')
           response = table.scan()
           pprint.pprint(response, depth=3, compact = True)
    
    mainMenu()  # <-- here
    
    
    """Selection number 2 Calculate exersises for beginners """     
    def beginner():
        ...
    

    您在定义所有其他功能之前执行mainMenu,因此仅定义了coach

    删除对mainMenu的那些调用,以仅将其保留在末尾:

    if __name__ == '__main__':
        mainMenu()
    

    回答来源:stackoverflow

    2020-03-25 00:21:30
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
动态、高效,蚂蚁动态卡片的内核逻辑 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载