Python Question / Quiz; What is the output of the following Python code, and why? 🤔🚀 Comment your answers below! 👇 #python #programming #developer #morioh #programmer #coding #coder #webdeveloper #webdevelopment #pythonprogramming #pythonquiz #machinelearning

Python_Dv's tweet image. Python Question / Quiz;

What is the output of the following Python code, and why? 🤔🚀 Comment your answers below! 👇

#python #programming #developer #morioh #programmer #coding #coder #webdeveloper #webdevelopment #pythonprogramming #pythonquiz #machinelearning

the answer is 100, the code error x=+1 is not caught in the def. fun() is executed


The correct answer is A. Error. The variable x is defined inside the function fun() and thus is local to that function. The line x += 1 outside the function attempts to modify x, which does not exist in the global scope, resulting in a NameError.


B. If X was declared a global variable inside the function def fun(): global X print(X) X =X+1 fun() Then here the output would have been 101


It will generate a NameError on the line x =+ 1 since ‘x’ is not defined within the modules namespace.


I mean, it's ALWAYS going to return 100, regardless of local/global variables. The function literally sets x to 100 then prints it. A better trick would be make the answer 1 because that's what you get if you print x after x=+1 (Not the same as x += 1)


In +1, the + is a unary operater, thus it does not change the value of x, hence just tells that x is a positive no. fun()'s x is a local variable & x=+1 is a global variable when fun() is c/d then function will print local variable not the global. output: 100


x =+1 # x <- 1 # When fun() is called # Inside fun(), x is a local variable # x <- 100 # 100 <- print(x) # The answer is: # B. 100


100 is the answer I hope I am correct and analyzed properly fun() called and executed and x=+1 was not part of fun()


The x =+ 1 is called outside of the function so the answer is B. 100


b) 100 cuando corre la funcion fun(), le asigna 100 a x y luego la imprime


It's 100. It's probably for "x=+1" was called outside of the 'fun' function, therefore the 'fun' did not call it.

DaddyZ1990's tweet image. It's 100.

It's probably for "x=+1" was called outside of the 'fun' function, therefore the 'fun' did not call it.

x is not defined, you just get an error


Option B (100) is the correct answer. The function "func" creates a variable x which equals 100 and prints it. Therefore when the function is called it does exactly that


United States Trends
Loading...

Something went wrong.


Something went wrong.