zoukankan      html  css  js  c++  java
  • 【笔试题】局部变量和全局变量

    请说出运行结果,并解释why?

     

     ================第一部分==================

    res = None
    def calc(a,b):
        res = a+b
    calc(1,2)
    print(res) 

    上面代码结果是:

    res = None
    def calc(a,b):
        res = 0
        res = a+b
    calc(1,2)
    print(res)

    上面代码结果是:

    res = None
    def calc(a,b):
        global res
        res = a+b
    calc(1,2)
    print(res)

    上面代码结果是:

    函数外部未定义res

    res2 = None
    def calc(a,b):
        global res
        res = a+b
    calc(1,2)
    print(res)
    

    上面代码结果是:  

    res = None
    def calc(a,b):
        res = a+b
        global res
    calc(1,2)
    print(res)

    上面代码结果是:

    res = None
    def calc(a,b):
        global res
        res = 0
        res = a+b
    calc(1,2)
    print(res)

    上面代码结果是:

    res = None
    def calc(a,b):
        res = 0
        global res
        res = a+b
    calc(1,2)
    print(res) 

    上面代码结果是:

    res = None
    def calc(a,b):
        res = 0    
        res = a+b
        global res
    calc(1,2)
    print(res)

    上面代码结果是:

    money = 0
    def tom():
        global money
        money = 100
    
    def jack():
        global money
        money = money - 50
    tom()
    jack()
    print('jack消费后剩余%s'%money) 

    上面代码结果是:

    函数外部未定义money

    def tom():
        global money
        money = 100
    
    def jack():
        global money
        money = money - 50
    tom()
    jack()
    print('jack消费后剩余%s'%money)
    

    上面代码结果是:  

     ================第二部分================== 

    d = {}
    def test():
        d['url']='https://www.cnblogs.com/uncleyong/p/10530261.html'
    def test2():
        d['url']='https://www.cnblogs.com/uncleyong/'
    test()
    test2()
    print(d)

    上面代码结果是:

    def test():
        d = {}
        d['url']='https://www.cnblogs.com/uncleyong/p/10530261.html'
    def test2():
        d = {}
        d['url']='https://www.cnblogs.com/uncleyong/'
    test()
    test2()
    print(d)

    上面代码结果是:

    def test():
        global d
        d = {}
        d['url']='https://www.cnblogs.com/uncleyong/p/10530261.html'
    def test2():
        d = {}
        d['url']='https://www.cnblogs.com/uncleyong/'
    test()
    test2()
    print(d)  

    上面代码结果是:

    def test():
        d = {}
        d['url']='https://www.cnblogs.com/uncleyong/p/10530261.html'
    def test2():
        global d
        d = {}    
        d['url']='https://www.cnblogs.com/uncleyong/'
    test()
    test2()
    print(d)

    上面代码结果是:

    def test():
        global d
        d = {}
        d['url']='https://www.cnblogs.com/uncleyong/p/10530261.html'
    def test2():
        global d
        d = {}
        d['url']='https://www.cnblogs.com/uncleyong/'
    test()
    test2()
    print(d)  

    上面代码结果是:

    info ={'age':18, 'url':'https://www.cnblogs.com/uncleyong/p/10530261.html'}
    def test():
        global info
        info={}
        info['name']='qzcsbj'
    test()
    print(info)

    上面代码结果是: 

    info ={'age':18, 'url':'https://www.cnblogs.com/uncleyong/p/10530261.html'}
    def test():
        info={}
        info['name'] = 'qzcsbj'
    test()
    print(info)

    上面代码结果是: 

    info ={'age':18, 'url':'https://www.cnblogs.com/uncleyong/p/10530261.html'}
    def test():
        info['age']=info['age']+1
    test()
    print(info) 

    上面代码结果是:

    s = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
    def test():
        s = 'test'
    test()
    print(s)

    上面代码结果是: 

    url = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
    def test():
        s = 'test'
    test()
    print(s)

    上面代码结果是:

    url = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
    def test():
        global s
        s = 'test'
    test()
    print(s)

    上面代码结果是:

    s = 'https://www.cnblogs.com/uncleyong/p/10530261.html'
    def test():
        global s
        s = 'test'
    test()
    print(s) 

    上面代码结果是:

    s = [1,2,3]
    def test():
        s[0]= 123
    test()
    
    print(s)

    上面代码结果是:

    s = [1,2,3]
    def test():
        s = []
        s.append(123)
    test()
    
    print(s)

    上面代码结果是:  

    s = [1,2,3]
    def test():
        global s
        s[0]= 123
    test()
    
    print(s)

    上面代码结果是:

    s = [1,2,3]
    def test():
        global s
        s = []
        s.append(123)
    test()
    
    print(s)

    上面代码结果是: 

    s = (1,2,3)
    def test():
        s[0]=123
    test()
    print(s)

    上面代码结果是:

    s = (1,2,3)
    def test():
        global s
        s[0]=123
    test()
    print(s)

    上面代码结果是:

    s = (1,2,3)
    def test():
        s = (4,5)
    test()
    print(s)
    

    上面代码结果是: 

    s = (1,2,3)
    def test():
        global s
        s = (4,5)
    test()
    print(s)
    

    上面代码结果是:

    s = {1,2,3}
    def test():
        s.add(5)
    test()
    print(s)

    上面代码结果是:

    s = {1,2,3}
    def test():
        global s
        s.add(5)
    test()
    print(s)

    上面代码结果是:

    s = {1,2,3}
    def test():
        s = set()
        s.add(5)
    test()
    print(s)

    上面代码结果是:

    s = {1,2,3}
    def test():
        global s
        s = set()
        s.add(5)
    test()
    print(s)

    上面代码结果是:

  • 相关阅读:
    UCloud-201809-001:Redis服务未授权访问漏洞安全预警
    php框架tp3.2.3和js写的微信分享功能心得,分享的标题内容图片自定义
    一键分享到QQ空间、QQ好友、新浪微博、微信代码
    ArcGIS10.x Engine直连提示连接超时ORA-12170 来自:http://www.iarcgis.com/?p=1004
    ArcGIS Engine三维动画开发 来自:http://www.iarcgis.com/?p=826
    ArcGIS Engine断开其他ArcSDE用户连接的解决方案
    ARCGIS 10.0破解版安装过程error 1606 和error 1316问题 及安装流程
    教你如何查看CAD文件是哪个版本的来自http://blog.sina.com.cn/s/blog_4c9fa4dd0101il1v.html
    C# DataGridView,右键单击RowHeader时显示右键菜单怎么做?
    C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
  • 原文地址:https://www.cnblogs.com/uncleyong/p/11230413.html
Copyright © 2011-2022 走看看