Home > Net >  Problem when calling dictionary variables. TypeError: tuple indices must be integers or slices, not
Problem when calling dictionary variables. TypeError: tuple indices must be integers or slices, not

Time:04-06

I would like to print variable_1 of result1 and variable_3 of result2. I would like to try and continue to use similar syntax where I can specify the call of result1 and variable_1. I want to print and recall both conditions.

I get the following error: TypeError: tuple indices must be integers or slices, not str

How can I solve?

def a():
    result1 = {}  
    if 2 > 1:
        print("yes 1")
        result1['variable_1'] = 2 2
        result1['variable_11'] = 'yes11'
                  
    else:
        print("no 1")
        result1['variable_2'] = 'no1'



    result2 = {}            
    if 3 < 2:
        print("yes 2")
        result2['variable_3'] = 'yes2'
    else:
        print("no 2")
        result2['variable_4'] = 'no2'
        
    return result1, result2

result1 = a()
result2 = a()

print(result1['variable_1'])
print(result2['variable_3'])

CodePudding user response:

Your code as given has a() returns two values.

so what your code does is the following:

result1 = (result1, result2)
result2 = (result1, result2)    

where the values within the parenthesis are variables based on the variables within a().

What you can do:

def a():
    result1 = {}  
    if 2 > 1:
        print("yes 1")
        result1['variable_1'] = 2 2
        result1['variable_11'] = 'yes11'
                  
    else:
        print("no 1")
        result1['variable_2'] = 'no1'



    result2 = {}            
    if 3 < 2:
        print("yes 2")
        result2['variable_3'] = 'yes2'
    else:
        print("no 2")
        result2['variable_4'] = 'no2'
        
    return result1, result2

result1, result2 = a()

print(result1['variable_1'])
print(result2['variable_3'])

A better way to lessen the confusion:

def a():
    r1 = {"variable_1": None,
          "variable_11": None,
          "variable_2": None}  
    if 2 > 1:
        print("yes 1")
        r1['variable_1'] = 2 2
        r1['variable_11'] = 'yes11'
    else:
        print("no 1")
        r1['variable_2'] = 'no1'

    r2 = {"variable_3": None,
          "variable_4": None}            
    if 3 < 2:
        print("yes 2")
        r2['variable_3'] = 'yes2'
    else:
        print("no 2")
        r2['variable_4'] = 'no2'
        
    return r1, r2

result1, result2 = a()

print(result1['variable_1'])
print(result2['variable_3'])

The problem also stems from the fact that you don't have default values for variable_3 since in result2, 3 < 2 is False, so r2['variable_4'] is defined but r2['variable_3'] isn't.

I set the default values to None; but you can set it to anything you wish.

That said, while the above code works, I'm not entirely sure what you're trying to do.

CodePudding user response:

You are assigning two different variables both to the same thing: a tuple. What you really want is tuple unpacking. So instead of doing:

result1 = a()
result2 = a()

Do:

result1, result2 = a()

You do this because a() returns 2 different values, and you want to assign a var to each one. It’s equivalent to doing

result1 = a()[0]
result2 = a()[1]
  • Related