Python 从一个函数中调用2个函数

Python 从一个函数中调用2个函数,python,python-2.7,function,Python,Python 2.7,Function,我是一个相对的新手,正在努力帮助自己更好地理解函数。我为自己做了一个练习 编写一个简单的程序: 问3个简单的问题 根据用户输入响应每个Q(每个Q响应由3个唯一功能中的一个确定) 把这些答案放在最后一个简单的总结句中 我使用一个函数作为主“控制器”->questions() 我想调用questions(),然后从内部调用其他3个函数。我感觉我的函数需要参数——但我不确定如何(我曾尝试在各种函数中加入参数——但完全卡住了——返回错误(请参见下文)) 我的代码: def naming(): # fun

我是一个相对的新手,正在努力帮助自己更好地理解函数。我为自己做了一个练习

编写一个简单的程序:

  • 问3个简单的问题
  • 根据用户输入响应每个Q(每个Q响应由3个唯一功能中的一个确定)
  • 把这些答案放在最后一个简单的总结句中
  • 我使用一个函数作为主“控制器”->questions()

    我想调用questions(),然后从内部调用其他3个函数。我感觉我的函数需要参数——但我不确定如何(我曾尝试在各种函数中加入参数——但完全卡住了——返回错误(请参见下文))

    我的代码:

    def naming(): # function to respond to name as per user input 
        if in_name == 'David':
            print 'That\'s a cool name!!'
        elif in_name == 'Jane':
            print 'That\'s a great name!!'
        else:
            print 'That\'s an OK name!!!'
    
    def age(): # function to respond to age as per user input 
        if in_age > 60:
            print 'That\'s old!!'
        elif in_age < 15:
            print 'That\'s young!!'
        else:
            print 'That\'s neither young nor old!!'
    
    def loc(): # function to respond to location as per user input 
        if in_loc == 'London':
            print 'London is a big city!!'
        elif in_loc == 'Manchester':
            print 'Manchester is a wonderful place!!'
        else:
            print 'That sounds OK!!'
    
    
    def questions(): #function to own the whole process (name + age + loc)
        in_name = raw_input('What is your name? -->')
        naming()
        in_age = input('How old are you? -->')
        age()
        in_loc = raw_input('Where do you live? -->')
        loc()
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.'
    
    questions()
    
    def naming():#根据用户输入响应名称的函数
    如果in_name==“David”:
    打印“这是个很酷的名字!!”
    elif in_name==“Jane”:
    打印“这是个好名字!!”
    其他:
    打印“这是个不错的名字!!!”
    def age():#根据用户输入响应年龄的函数
    如果年龄>60岁:
    打印“那是旧的!!”
    年龄<15岁时的elif:
    打印“那太年轻了!!”
    其他:
    打印“既不年轻也不老!!”
    def loc():#根据用户输入响应位置的函数
    如果在_loc==“伦敦”:
    打印“伦敦是个大城市!!”
    elif in_loc==“曼彻斯特”:
    打印“曼彻斯特是个很棒的地方!!”
    其他:
    打印“听起来不错!!”
    def questions():#拥有整个流程的函数(name+age+loc)
    在\u name=raw\u输入中(“您的名字是什么?-->”)
    命名()
    in_age=input('你多大?-->'))
    年龄()
    in_loc=raw_input('你住在哪里?-->'))
    loc()
    打印“你的名字是”,在“你的名字”中,在“你的年龄”中,在“你的年龄”中,在“你的住所”中
    问题()
    
    我猜在主问题()函数中-我需要在命名/age/loc函数中提供某种形式的说明或参数

    非常感谢您的帮助!是的,这里还有一些类似的线索,但我已经读过了,没有一条对我有意义。 理想情况下,对我最有帮助的事情是,如果一些好心人能花3到4分钟来编辑我的代码,使其正确运行

    提前谢谢

    PS-这是我得到的错误截图
    函数
    questions()
    中的变量对于其他函数来说是未知的(或者它们的作用域被限制在
    questions()
    函数),这就是为什么会出现错误,而不是定义错误的原因

    def naming(in_name): # function to respond to name as per user input 
        if in_name == 'David':
            print 'That\'s a cool name!!'
        elif in_name == 'Jane':
            print 'That\'s a great name!!'
        else:
            print 'That\'s an OK name!!!'
    
    def age(in_age): # function to respond to age as per user input 
        if in_age > 60:
            print 'That\'s old!!'
        elif in_age < 15:
            print 'That\'s young!!'
        else:
            print 'That\'s neither young nor old!!'
    
    def loc(in_loc): # function to respond to location as per user input 
        if in_loc == 'London':
            print 'London is a big city!!'
        elif in_loc == 'Manchester':
            print 'Manchester is a wonderful place!!'
        else:
            print 'That sounds OK!!'
    
    
    def questions(): #function to own the whole process (name + age + loc)
        in_name = raw_input('What is your name? -->')
        naming(in_name)
        in_age = input('How old are you? -->')
        age(in_age)
        in_loc = raw_input('Where do you live? -->')
        loc(in_loc)
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.'
    
    questions()
    
    def命名(in_name):#根据用户输入响应名称的函数
    如果in_name==“David”:
    打印“这是个很酷的名字!!”
    elif in_name==“Jane”:
    打印“这是个好名字!!”
    其他:
    打印“这是个不错的名字!!!”
    def age(in#u age):根据用户输入响应age的函数
    如果年龄>60岁:
    打印“那是旧的!!”
    年龄<15岁时的elif:
    打印“那太年轻了!!”
    其他:
    打印“既不年轻也不老!!”
    def loc(in#loc):根据用户输入响应位置的功能
    如果在_loc==“伦敦”:
    打印“伦敦是个大城市!!”
    elif in_loc==“曼彻斯特”:
    打印“曼彻斯特是个很棒的地方!!”
    其他:
    打印“听起来不错!!”
    def questions():#拥有整个流程的函数(name+age+loc)
    在\u name=raw\u输入中(“您的名字是什么?-->”)
    命名(名称)
    in_age=input('你多大?-->'))
    年龄(单位:岁)
    in_loc=raw_input('你住在哪里?-->'))
    loc(in_loc)
    打印“你的名字是”,在“你的名字”中,在“你的年龄”中,在“你的年龄”中,在“你的住所”中
    问题()
    
    更好的方法是将它们作为参数传递给函数

    def naming(in_name): # function to respond to name as per user input 
        if in_name == 'David':
            print 'That\'s a cool name!!'
        elif in_name == 'Jane':
            print 'That\'s a great name!!'
        else:
            print 'That\'s an OK name!!!'
    
    def questions(): #function to own the whole process (name + age + loc)
        in_name = raw_input('What is your name? -->')
        naming(in_name)
    
    同样,变量
    在_age
    中,需要转换为
    int
    ,因为
    原始输入默认为
    字符串
    格式

    in_age = input('How old are you? -->')
    age(int(in_age))
    

    函数内部定义的变量是该函数的局部变量。它可以从定义时开始访问,直到函数结束,并且在函数执行期间一直存在

  • 您可以将这些值作为参数传递给其他函数

    def naming(in_name): # function to respond to name as per user input 
        if in_name == 'David':
            print 'That\'s a cool name!!'
        elif in_name == 'Jane':
            print 'That\'s a great name!!'
        else:
            print 'That\'s an OK name!!!'
    
    def age(in_age): # function to respond to age as per user input 
        if in_age > 60:
            print 'That\'s old!!'
        elif in_age < 15:
            print 'That\'s young!!'
        else:
            print 'That\'s neither young nor old!!'
    
    def loc(in_loc): # function to respond to location as per user input 
        if in_loc == 'London':
            print 'London is a big city!!'
        elif in_loc == 'Manchester':
            print 'Manchester is a wonderful place!!'
        else:
            print 'That sounds OK!!'
    
    
    def questions(): #function to own the whole process (name + age + loc)
        in_name = raw_input('What is your name? -->')
        naming(in_name)
        in_age = input('How old are you? -->')
        age(in_age)
        in_loc = raw_input('Where do you live? -->')
        loc(in_loc)
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.'
    
    questions()
    
    def命名(in_name):#根据用户输入响应名称的函数
    如果in_name==“David”:
    打印“这是个很酷的名字!!”
    elif in_name==“Jane”:
    打印“这是个好名字!!”
    其他:
    打印“这是个不错的名字!!!”
    def age(in#u age):根据用户输入响应age的函数
    如果年龄>60岁:
    打印“那是旧的!!”
    年龄<15岁时的elif:
    打印“那太年轻了!!”
    其他:
    打印“既不年轻也不老!!”
    def loc(in#loc):根据用户输入响应位置的功能
    如果在_loc==“伦敦”:
    打印“伦敦是个大城市!!”
    elif in_loc==“曼彻斯特”:
    打印“曼彻斯特是个很棒的地方!!”
    其他:
    打印“听起来不错!!”
    def questions():#拥有整个流程的函数(name+age+loc)
    在\u name=raw\u输入中(“您的名字是什么?-->”)
    命名(名称)
    in_age=input('你多大?-->'))
    年龄(单位:岁)
    in_loc=raw_input('你住在哪里?-->'))
    loc(in_loc)
    打印“你的名字是”,在“你的名字”中,在“你的年龄”中,在“你的年龄”中,在“你的住所”中
    问题()
    
  • 否则,您可以将变量定义为global,这样就不必将其作为参数传递。(不建议使用此方法)

    def naming():#根据用户输入响应名称的函数
    如果in_name==“David”:
    打印“这是个很酷的名字!!”
    elif in_name==“Jane”:
    打印“这是个好名字!!”
    其他:
    打印“这是个不错的名字!!!”
    def age():#根据用户输入响应年龄的函数
    如果年龄>60岁:
    打印“那是旧的!!”
    年龄<15岁时的elif:
    打印“那是年轻的”
    
    def naming(): # function to respond to name as per user input 
        if in_name == 'David':
            print 'That\'s a cool name!!'
        elif in_name == 'Jane':
            print 'That\'s a great name!!'
        else:
            print 'That\'s an OK name!!!'
    
    def age(): # function to respond to age as per user input 
        if in_age > 60:
            print 'That\'s old!!'
        elif in_age < 15:
            print 'That\'s young!!'
        else:
            print 'That\'s neither young nor old!!'
    
    def loc(): # function to respond to location as per user input 
        if in_loc == 'London':
            print 'London is a big city!!'
        elif in_loc == 'Manchester':
            print 'Manchester is a wonderful place!!'
        else:
            print 'That sounds OK!!'
    
    
    def questions(): #function to own the whole process (name + age + loc)
        global in_name, in_age, in_loc
        in_name = raw_input('What is your name? -->')
        naming()
        in_age = input('How old are you? -->')
        age()
        in_loc = raw_input('Where do you live? -->')
        loc()
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.'
    
    questions()