Select the correct answerWhat will be the output of the following Python code?def ct(y): global y y+=2 print(y)ct(5)print("hi")Options7hierror7hi
Question
Select the correct answerWhat will be the output of the following Python code?def ct(y): global y y+=2 print(y)ct(5)print("hi")Options7hierror7hi
Solution
The correct answer is "error".
This is because in Python, you cannot use the 'global' keyword for a function parameter. The 'global' keyword is used to modify a global variable inside a function. But in this case, 'y' is a local variable because it is a function parameter. Therefore, using 'global y' will cause an error.
Similar Questions
What will be the output of the following Python code?def ct(y): global y y+=2 print(y)ct(5)print("hi")Optionserrorhi77hi
Select the correct answerWhat will be the output of the following Python code?def ct(): y=9 print(y)y=7ct()Options97Error97
Select the correct answerWhat will be the output of the following Python code?def ct(): global y y+=3 print(y)y=8print("y")OptionsError11yy11
Select the correct answerWhat will be the output of the following Python code?def ct(): global x print(x) x = "hii" print(x) x = "welcome" ct()print(x)Optionshiiwelcomewelcomewelcomehiihiihiihiiwelcomewelcomehiiwelcome
Select the correct answerWhat will be the output of the following Python code?def ct(y): print(y+2)y=-6y=3ct(8)Options10435
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.