将信息从功能的一个部分传递到另一个部分的问题

时间:2013-12-04 08:10:30

标签: python variables dictionary average

在处理我的程序时,我遇到了一个问题,即菜单选项1中存储的信息没有传输到菜单选项2.如您所见,它在菜单1中正确存储。当它返回到菜单选项2时,它就像从未进入选项1一样。

更新#1: 我有一些建议是了解范围?从我可以告诉程序,即使我在每个定义中输入了返回,程序也没有将数据传递给它的父程序。

#Must be able to store at least 4 grades
#Each class can have up to 6 tests and 8 hw's
#Weighted 40%*testavg 40% hw average attendance is 20%
#User must be able to input a minimum grade warning
#after each test the your program must calculate the students average and issue warning if necessary

##Define the Modules##
import math

def menu (a): #2nd thing to happen 
    menuend = 'a'
    while menuend not in 'e':
        menuend = raw_input("Type anything other then 'e' to continue:\n")
        print "What would you like to do ?"
        menudo = 0
        print "1 - Enter Courses\n2 - Select Course to Edit\n3 - Save File\n4 - Load File\n5 - Exit\n"
        menudo = input("Enter Selection:")
        if (menudo == 1):
            menuchck = 0
            menuchck = raw_input("\nYou have entered #1 (y/n)?:\n")
            if menuchck in ["Yes","yes","y","Y"]:
                x = m1()
            else:
                print "I'm sorry,",nam,",for the confusion, lets try again\n"
                menu()
        elif (menudo == 2):
            menuchck1 = 0
            menuchck1 = raw_input("\nYou have entered #2 (y/n)?:\n")
            if menuchck1 in ["Yes","yes","y","Y"]:
                x = m2()
            else:
                print "I'm sorry,",nam,",for the confusion, lets try again\n"
                menu()
        elif (menudo == 3):
            print "Entered 3"
        elif (menudo == 4):
        print "Entered 4"
        else:
            print "Anything Else Entered"

def course(): #3rd thing to happen 
    b = {}
    while True:
        while True:
            print "\n",name,", please enter your courses below ('e' to end):"
            coursename = raw_input("Course Name:")
            if (coursename == 'e'):
                break
            will = None
            while will not in ('y','n'):
                will = raw_input('Ok for this name : %s ? (y/n)' % coursename)
            if will=='y':
                b[coursename] = {}
        print "\n",name,", current course load:\n",b
        coursechck = None
        while coursechck not in ('y','n'):
            coursechck = raw_input("Are your courses correct (y/n)")
        if coursechck =='y':
            return b
        else:
            b = {}
            print

##Menu Options##
def m1():
    a = course()
    return a

def m2():
    print "Excellent",name,"lets see what courses your enrolled in\n"
    print x
    return x

###User Input Section###
name = raw_input("Enter Students Name:\n")
a = {}
menu(a)

raw_input("This is the end, my only friend the end")

1 个答案:

答案 0 :(得分:0)

在do == 1案例中的if-elif块中,您编写m1(),但在最后一种情况下,您编写x=m1()。你应该把后者放在任何地方(通过键入m1()你只运行该函数,但不要将返回的x存储在任何地方。)

顺便说一句,你可以使用if chck in ["Yes","yes","Y","y"]:

来避免这种if-elif混淆