• Register
1 vote
92 views

Problem:

I have a simple python program that supposes to check and compare different numbers. Here is my sample code below

myVar1 = 1
myVar2 = 0
def myFunction(): 
    if myVar2 == 0 and myVar1 > 0:
        print("This is the result of if block")
    elif myVar1 == 1 and myVar1 > 0:
        print("This is the result of elif block")
    elif myVar1 < 1:
        print("This is the result of another elif block")
    myVar1 =- 1
myFunction()

This program throws an error UnboundLocalError: local variable ‘myVar1’ referenced before assignment. Do you guys have any idea, where I am doing wrong? Please help me to fix the error.

Thanks

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes

Solution:

In python, If you set the value of a variable inside the function, python understands it as a local variable with that name. Hence, you declared myVar1 inside the function the compiler read it as a local variable, thus the reason you are getting the error.

To avoid this error you may add global keyword in front of the variable inside the function.

def myFunction():
    global myVar1
    global myVar2

Or you may pass parameters inside the function

def myFunction(myVar1, myVar2): 
    if myVar2 == 0 and myVar1 > 0:
        print("This is the result of if block")
    elif myVar1 == 1 and myVar1 > 0:
        print("This is the result of elif block")
    elif myVar1 < 1:
        print("This is the result of another elif block")
    myVar1 =- 1
myFunction(1,0)

Thanks.

13 9 6
94,260 points

Related questions

0 votes
1 answer 38 views
38 views
Problem: The following code gives the error UnboundLocalError: local variable 'Var1' referenced before assignment: Var1 = 1 Var2 = 0 def function(): if Var2 == 0 and Var1 > 0: print("Result One") elif Var2 == 1 and Var1 > 0: print("Result Two") elif Var1 < 1: print("Result Three") Var1 =- 1 function() How can I fix this? Thanks for any help!
asked Jan 8 Mashhoodch 13k points
1 vote
1 answer 3 views
3 views
Problem: Please help me. How can I solve it? python local variable referenced before assignment
asked Mar 19 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 35 views
35 views
Problem: Hi! I am a novice and trying to catch up the concepts in Java. I tried to access a local variable without assigning it a value in main() method and got following error: error: variable might not have been initialized Meanwhile, if I declare the ... variable or the same value is assigned to all variables regardless of datatype? If any of you could explain this, I would be thankful.
asked Nov 29, 2020 Code Learner 9.5k points
0 votes
1 answer 5 views
5 views
Problem: How I can fix this issue: What are local and global variable in python?
asked Mar 17 Wafa Abu Yousef 6.1k points
0 votes
2 answers 26 views
0 votes
1 answer 20 views
20 views
Problem: Hi there! I am trying to run the following program which is giving the above-mentioned warning: #include<iostream> #include<conio.h> using namespace std; int add(int number1, int number2) { int result = number1 + number2; return &result; } int subtract (int number1, ... When I run this program, it gives the same warning on different lines. I do not know how to solve this issue. Any help?
asked Feb 24 Code Learner 9.5k points
1 vote
1 answer 69 views
69 views
Problem: Hello experts, I seek your attention. I am a new student in C++ programming. I have installed all the essential software and about the give my first shot using this programming language. Unfortunately, I&rsquo;ve encountered an error and that says uninitialized local variable ' ... be a noob question. I know that, but I want to know where am I doing the wrong stuff? Thanks for your help.
asked May 19, 2020 adamSw 11.3k points
1 vote
2 answers 625 views
625 views
Problem: Hello, Kodlogs, I am a novice in java. I am trying to catch up on things in Java programming language. Thus I write my very first program and try to run it. But the problem comes when I run my program. It is not giving any output except an error &ldquo;The value of the local ... { int x = 12; int y = 10; double num = 9.20; String s = "This is my String"; } } Thanks for your kind help
asked Mar 28, 2020 Gavin 15.3k points
0 votes
1 answer 10 views
10 views
Problem: If I have a class OnlineDataRetrieval which I need to use time to time in my Activity, is it wise to open a global object for that class and reuse that object throughout the lifespan of the activity, Or I should just create a local Object every time I need to use that class? So basically I want to know which of the options below is preferable from performance point of view?
asked Dec 5, 2020 Mashhoodch 13k points
0 votes
1 answer 35 views
35 views
Problem: What are local and global objects in C++? What is their difference?
asked Oct 14, 2020 Code Learner 9.5k points