从函数

时间:2017-08-02 12:07:40

标签: python python-2.7 function

我是一个相对新手,我正在努力帮助自己更好地理解功能。我为自己创造了一个练习

写一个简单的程序:

  1. 问3个简单的Qs
  2. 根据用户输入(每个Q响应由3个独特功能中的一个确定)响应每个Q
  3. 将这些答案放在最后的简单摘要句中
  4. 我正在使用一个功能作为主'控制器' - >的问题()

    我想调用问题(),然后从内部调用其他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()
    

    我猜在主要的 questions()函数中 - 我需要在命名/ age / loc 函数中提供某种形式的指令或参数

    非常感谢这里的一些帮助!是的 - 这里有一些其他类似的线程 - 但我已经读过它们,没有一个对我有意义。 理想情况下 - 对我来说最有用的事情是,如果一些好的撒玛利亚人可以花3到4分钟来编辑我的代码以便正确操作。

    提前致谢!

    PS - 这是我得到的错误的屏幕截图 error

3 个答案:

答案 0 :(得分:0)

函数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()

答案 1 :(得分:0)

更好的方法是将它们作为参数传递给函数。

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)

再次变量in_age,默认情况下需要转换为int raw_input string格式。

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

答案 2 :(得分:0)

在函数内定义的变量是该函数的本地变量。它可以从定义它的位置访问,直到函数结束,并且只要函数正在执行就存在。

  1. 您可以将值作为参数传递给其他函数

    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()
    
  2. 否则您可以将变量定义为全局,因此您不必将其作为参数传递。(建议不要使用此方法)

    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()