I am trying to learn python and I have just gotten past conditional statements and I'm working on creating my own functions.
Would you mind telling me what I am doing wrong that I have to write the convoluted print statement at the end that calls all of my functions one by one?
Also any style tips would also be greatly appreciated.
Thanks in advance for anything you can provide.
import random
#VARIABLES
topChamps = ['Nasus', 'Garen', 'Nautilus']
botChamps = ['Draven', 'Caitlyn', 'Jhin']
midChamps = ['Lux', 'Veigar', 'Karthus']
supChamps = ['Sona', 'Tahm Kench', 'Alistar']
jgChamps = ['Amumu', 'Rammus', 'Malphite']
selectNum = 0
roleNum = 0
champArr = []
champ = "wrong"
role = str(input("what role did you get? "))
#function to make the role choice easier for computer to read
def roleChoose():
if role == "top":
roleNum = 1
elif role == "bot":
roleNum = 2
elif role == "mid":
roleNum = 3
elif role == "sup":
roleNum = 4
elif role == "jg":
roleNum = 5
return roleNum
#function that determines what role list to use for champion choice
def arrayGrab(Num):
if Num == 1:
champArr = topChamps
elif Num == 2:
champArr = botChamps
elif Num == 3:
champArr = midChamps
elif Num == 4:
champArr = supChamps
elif Num == 5:
champArr = jgChamps
return champArr
print("made it to the end but you messed up")
#function that pulls a random name from champ list
def charSelect(arr):
x = random.randrange(0,2)
champ = arr[x]
return champ
print(charSelect(arrayGrab(roleChoose())))
CodePudding user response:
IIUC, you can unnest your print like this:
role = roleChoose()
grab = arrayGrab(role)
char = charSelect(grab)
print(char)
Use any variable names you like on the left hand sides of the assignments.
CodePudding user response:
In python programs there are different namespaces. For instance, there is the global namespace, and each function also has its own namespace. In your program, the same variable name is used across different contexts, but these variables are not shared.
Consider this program:
roleNum = 0
def roleChoose():
roleNum = 1
return roleNum
roleChoose()
The first roleNum is a global variable. When roleChoose is called, a new namespace is created. All new variables in roleChoose are constrained to this namespace. When the function returns, it leaves the global roleNum untouched.
To retrieve the value of a variable, the python interpreter will first check the local namespace, and then the outer namespaces. Because of this, it is possible to use a global variable if it has not been overshadowed by a local variable. I.e.
role = "top"
def roleChoose():
if role == "top":
roleNum = 1
In the function, role is still accessible. The function does not overwrite values in the global scope, unless it is explicitly stated otherwise. This is done using the global
keyword. I.e.
roleNum = 0
def roleChoose():
global roleNum
roleNum = 1
return roleNum
roleChoose()
Alternatively, you could assign the return value in the global scope
roleNum = 0
def roleChoose():
roleNum = 1
return roleNum
roleNum = roleChoose()
The latter is in general preferred, as the risk of unintended side-effects and errors is lower.
Using this logic we can rewrite:
print(charSelect(arrayGrab(roleChoose())))
to
roleNum = roleChoose()
champArr = arrayGrab(roleNum)
champ = charSelect(champArr)
print(champ)
CodePudding user response:
import random
#VARIABLES
topChamps = ['Nasus', 'Garen', 'Nautilus']
botChamps = ['Draven', 'Caitlyn', 'Jhin']
midChamps = ['Lux', 'Veigar', 'Karthus']
supChamps = ['Sona', 'Tahm Kench', 'Alistar']
jgChamps = ['Amumu', 'Rammus', 'Malphite']
selectNum = 0
roleNum = 0
champArr = []
champ = "wrong"
role = input("what role did you get? ")
#function to make the role choice easier for computer to read
def roleChoose(choose):
if role == "top":
roleNum = 1
elif role == "bot":
roleNum = 2
elif role == "mid":
roleNum = 3
elif role == "sup":
roleNum = 4
elif role == "jg":
roleNum = 5
return roleNum
#function that determines what role list to use for champion choice
def arrayGrab(Num):
if Num == 1:
champArr = topChamps
elif Num == 2:
champArr = botChamps
elif Num == 3:
champArr = midChamps
elif Num == 4:
champArr = supChamps
elif Num == 5:
champArr = jgChamps
return champArr
print("made it to the end but you messed up")
#function that pulls a random name from champ list
def charSelect(arr):
x = random.randrange(0,2)
champ = arr[x]
return champ
choose=roleChoose("top")
print(choose)
print(charSelect(arrayGrab(choose)))