将信息传递到菜单内功能的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将信息传递到菜单内功能的问题相关的知识,希望对你有一定的参考价值。

这是我正在处理的菜单的一部分。我能够获得第一个选择(“教练”的第一选择);但是,我在其他所有选择上都有困难。当我在菜单中选择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()
答案

您在每个函数定义下调用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()

以上是关于将信息传递到菜单内功能的问题的主要内容,如果未能解决你的问题,请参考以下文章

使用导航从工具栏菜单项单击在片段之间传递数据 - Kotlin

选择导航菜单项时不会替换上一个片段

从一个片段传递到另一个片段

如果几何着色器处于活动状态,如何将信息从顶点着色器传递到片段着色器?

在 NavigationUI 的同一主机活动上传递数据并更新片段文本/按钮

将数据从底部工作表对话框片段传递到片段