-3
def para(value):

    if(value==1):
        a1=10
        b1=11
        c1=12
        d1=13
    elif(value==2):
        a1=20
        b1=21
        c1=22
        d1=23
    elif(value==3):
        a1=30
        b1=31
        c1=32
        d1=33
    else:
        print("wrong input")


    dict_a=dict({'a':a1,'b':b1})
    dict_b=dict({'c':c1,'d':d1})

    return(dict_a, dict_b)

def main():
    global dict_a, dict_b

    value=input("enter the choice: \n1. 1st \n2. 2nd \n3. 3rd \n4. 4th")

    [dict_a, dict_b]=para(value)

main()

The error that I get is:

dict_a=dict({'a':a,'b':b})
UnboundLocalError: local variable 'a1' referenced before assignment
khelwood
  • 55,782
  • 14
  • 81
  • 108
Nandu
  • 57
  • 7
  • `value` is a string, not an int, so it is not equal to 1, 2 or 3, so `a1` is never assigned a value. – khelwood Sep 10 '19 at 10:04
  • 3
    If it hits `else`, no variable is made and yet `dict_a` and `dict_b` uses `a1` and `b1` regardless. Try making variables in `else`, too, or raise something to prevent further going. – Chris Sep 10 '19 at 10:04
  • See [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – khelwood Sep 10 '19 at 10:04
  • Additionally: You don't need the parantheses for an `if`statement (it's not a function call). Change `if(value==1):` to `if value == 1:`. Ansd please get rid of the `global`. – Matthias Sep 10 '19 at 10:07
  • @khelwood i've made the input as int and i've ended up with this TypeError: int() takes 0 positional arguments but 1 was given – Nandu Sep 10 '19 at 11:01
  • @Nandu Feel free to post a question with a [mre] about your new problem. – khelwood Sep 10 '19 at 11:02

2 Answers2

0

If your code hits the else block, a1 won't be assigned a value.

You can fix this in one of the following two ways:

Assign a default value in case it doesn't get assigned in the conditional blocks.

a1 = 0
b1 = 0
c1 = 0
d1 = 0
if(value==1):
    a1=10
    b1=11
    c1=12
    d1=13
elif(value==2):
    a1=20
    b1=21
    c1=22
    d1=23
elif(value==3):
    a1=30
    b1=31
    c1=32
    d1=33
else:
    print("wrong input")

Or assign the variables in the else block which will achieve basically the same thing

if(value==1):
    a1=10
    b1=11
    c1=12
    d1=13
elif(value==2):
    a1=20
    b1=21
    c1=22
    d1=23
elif(value==3):
    a1=30
    b1=31
    c1=32
    d1=33
else:
    a1 = 0
    b1 = 0
    c1 = 0
    d1 = 0
    print("wrong input")

If you don't want the user to use these default values, you can always throw an exception in the else that lets the user know they've inputted the wrong values

I_Adze
  • 89
  • 1
  • 7
0

You should rather initialise the dictionary only in case of valid input

def para(value):
    dict_a = dict()
    dict_b = dict()
    if(value==1):
        dict_a={'a':10,'b':11}
        dict_b={'c':12,'d':13}
    elif(value==2):
        dict_a={'a':20,'b':21}
        dict_b={'c':22,'d':23}
    elif(value==3):
        dict_a={'a':30,'b':31}
        dict_b={'c':32,'d':33}
    else:
        print("wrong input")

    return(dict_a, dict_b)