这是我正在处理的菜单的一部分。我能够获得第一个选择(“教练”的第一选择);但是,我在其他所有选择上都有困难。当我在菜单中选择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
您可以在每个函数定义下调用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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。