单选题 适中
#18198

执行下面Python代码后,输出的结果是?( ) x = 5 def foo ( ) : def bar ( ) : global x x = 10 bar (…

执行下面Python代码后,输出的结果是?( )

x = 5

def foo():
	def bar():
		global x
		x = 10
		
	bar()
	print(x, end="#")
	
foo()
print(x, end="#")
未登录也能判分,登录后才会记录
答案与解析
正确答案:D
解析

当调用bar() 时,它会将全局变量x 设置为10。因此,在foo() 内部的print(x, end=“#”)输出10,然后当foo() 完成后,再次输出print(x, end=“#”)时,x仍然是10。所以最终的输出是10#10#