繁体   English   中英

将信息从功能的一部分转移到另一部分时出现问题

[英]Problems transferring information from one part of a function to another

在我的程序上工作时,我遇到了一个问题,即菜单选项1中存储的信息没有转移到菜单选项2中。如您所见,当在菜单一中时,信息已正确存储。 当它返回到菜单选项2时,就像从未去过选项1一样。

更新#1:我的一些建议是了解范围? 根据我的判断,即使我在每个定义中都键入return,程序也不会将数据传递给其父程序。

#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")

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

顺便说一下,您可以if chck in ["Yes","yes","Y","y"]:使用if chck in ["Yes","yes","Y","y"]:来避免if-elif混淆if chck in ["Yes","yes","Y","y"]:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM