-4

I am trying to create a new list performing element-wise substruction of two python lists as follows:

from operator import add

number_villains_players = 0
villain_strength = []
player_strength = []
resulten_strength = []

def get_villain_strength(size):
    villain_strength = [int(x) for x in input("Enter {} numbers of space separated strength of Villains:".format(size)).split()]
    print(villain_strength)

def get_player_strength(size):

    player_strength = [int(x) for x in input("Enter {} numbers of space separated energy of Players:".format(size)).split()]
    print(player_strength)

def compare_strength():
    #resulten_strength = [m-n for (m,n) in zip(player_strength,villain_strength)]     #doesn't work
    #resulten_strength = [sum(x) for x in zip(player_strength, villain_strength)]     #doesn't work
    #resulten_strength = [list( map(add, player_strength, villain_strength) )]     #doesn't work
    resulten_strength = [a*b for a,b in zip(player_strength,villain_strength)]     #doesn't work
    print(resulten_strength)

def main():
    number_villains_players = input("How many Players/Villains?:")
    get_villain_strength(number_villains_players)
    get_player_strength(number_villains_players)
    compare_strength()
    if (i > 0 for i in resulten_strength):
        print("WIN")
    else:
        print("LOSE")

main()

But print(resulten_strength) is always empty as [] or [[]]

I have followed all the possible solutions from:

Can anyone point me where I am going wrong?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

5

You are assigning local lists in methods and they are not global. Thus the top lines defined are always empty. This should fix your problem:

def get_villain_strength(size):
    global villain_strength
    villain_strength = [int(x) for x in input("Enter {} numbers of space separated strength of Villains:".format(size)).split()]
    print(villain_strength)

However it's bad to use globals anywhere. You may want a function with something return.


When you are assigning the same name variable inside a function it will override the global variable's name, until you return from the function. Or to say if you lookup a variable, it first looks up the name in locals(), if nothing found, it'll go globals(). If still nothing found, an exception will be raised.

knh190
  • 2,744
  • 1
  • 16
  • 30
  • You see I need to access `villain_strength` created in `get_villain_strength()` to be accessed within `compare_strength()`. So is `player_strength` created in `get_player_strength()` to be accessed within `compare_strength()`. Finally, I need to access`resulten_strength` within `main()`. How would I be able to access without declaring as a **global** variable? – undetected Selenium Apr 12 '19 at 07:34
  • @DebanjanB use `return`. – knh190 Apr 12 '19 at 07:35
  • @DebanjanB when you are not assigning the same name variable in a function, you should be able to access the global variable. – knh190 Apr 12 '19 at 07:36
3

I'm not sure this is what you're looking for. I made a small modification to your code. I remove your variables declaration and modify your function.

def get_villain_strength(size):
    villain_strength = [int(x) for x in input("Enter {} numbers of space separated strength of Villains:".format(size)).split()]
    print (villain_strength)
    return(villain_strength)

def get_player_strength(size):

    player_strength = [int(x) for x in input("Enter {} numbers of space separated energy of Players:".format(size)).split()]
    print (player_strength)
    return(player_strength)

def compare_strength(x,y):        
    resulten_strength = [a*b for a,b in zip(x,y)]     
    return(resulten_strength)

def main():
    number_villains_players = input("How many Players/Villains?:")
    x = get_villain_strength(number_villains_players)
    y = get_player_strength(number_villains_players)
    print (compare_strength(x,y))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
YusufUMS
  • 1,506
  • 1
  • 12
  • 24