Home > Enterprise >  Calling multiple values from inside a function
Calling multiple values from inside a function

Time:10-16

Im trying to call for some values from inside of a function but global is giving me more errors

def init():
    Rounds = input("enter the amount of rounds: ")
    while Rounds.isnumeric() == False or int(Rounds) <= 0:
        Rounds = input("enter a valid value: ")
    Rounds = int(Rounds)

for i in range (Rounds):
    None

its not just one variable, I have about 6 that I need to call after the function. here Rounds in the for loop is showing an error saying it is not defined.

CodePudding user response:

The issue is Rounds is a local variable and is only defined inside init to fix that you can make it global with:

def init():
    global Rounds
    Rounds = input("enter the amount of rounds: ")
    while Rounds.isnumeric() == False or int(Rounds) <= 0:
        Rounds = input("enter a valid value: ")
    Rounds = int(Rounds)

for i in range(Rounds):
    None

CodePudding user response:

If need to access Global variables, need to mention global before them

Rounds = None

def init():
    global Rounds
    Rounds = input("enter the amount of rounds: ")
    while Rounds.isnumeric() == False or int(Rounds) <= 0:
        Rounds = input("enter a valid value: ")
    Rounds = int(Rounds)

init() # call method to populate Rounds

for i in range (Rounds):
    None

Safer way

def init():
    Rounds = input("enter the amount of rounds: ")
    while Rounds.isnumeric() == False or int(Rounds) <= 0:
        Rounds = input("enter a valid value: ")
    Rounds = int(Rounds)
    return Rounds


for i in range (init()):
    None

CodePudding user response:

No need to use global, using global is a bad practice. Just return the value from init:

def init():
    rounds = input("enter the amount of rounds: ")
    while rounds.isnumeric() == False or int(rounds) <= 0:
        rounds = input("enter a valid value: ")
    return int(rounds)

Rounds = init()
for i in range(Rounds):
    print(i)

CodePudding user response:

In addition to other answers, it's not a good idea to make a local variable in a function a global one. In fact, the global keyword should be used as little as possible. It can cause trouble in some cases. You have a better way in achieving this:

def init():
    global Rounds
    Rounds = input("enter the amount of rounds: ")
    while Rounds.isnumeric() == False or int(Rounds) <= 0:
        Rounds = input("enter a valid value: ")
    return int(Rounds)

for i in range(init()):
    None

Or if you want to use Rounds elsewhere, try this:

def init():
    global Rounds
    Rounds = input("enter the amount of rounds: ")
    while Rounds.isnumeric() == False or int(Rounds) <= 0:
        Rounds = input("enter a valid value: ")
    return int(Rounds)

# In python 3.8 
for i in range(Round := init()):
    None

# In python 3.7
Round = init()
for i in range(Round):
    None

CodePudding user response:

def init():
global Rounds
Rounds = input("enter the amount of rounds: ")
while Rounds.isnumeric() == False or int(Rounds) <= 0:
    Rounds = input("enter a valid value: ")
Rounds_int = int(Rounds)

for i in range(Rounds_int):
    pass

You needed to add global and in for i in range (Rounds): None you had space

  • Related