0

Py3 passing variable frustrating me

I am having a heck of a time getting this code to work can someone pleae help? def CreationSetup(value): print(numchars) def CharacterCreation(): CCCheck = input( "Is this character creation?") if CCCheck == "yes": numchars = input("How many characters would you like to create?") CreationSetup(numchars) if CCCheck == "no": print("Bite") else: print('Please enter valid input!\n') CharacterCreation() CharacterCreation() for some reason the program refuses to pass numchars between the 2 functions, I have globaled it, declared it outside the function, and 4 or 5 other variations. I get error code e1121 and every tutorial I look at asay it should be as simple as returning numchars (which I tried to no avail), or calling a function with the numcharas variable as a parameter...but when I do that it keeps telling me the variable is undefined even though the only way to get to the function that ues it as a variable is a path that requires giving the numchars variable a value...please help

10th Jul 2018, 12:15 PM
Kevin
14 odpowiedzi
0
I guess documentation would help #def to prove that variable has been correctly passed before moving on with coding def CreationSetup(value): print(numchars) #first function in program to determine nature of the execution (new character or advancement of old Character) def CharacterCreation(): CCCheck = input( "Is this character creation?") if CCCheck == "yes": #if CCCheck is yes assign numchars with a numeric value to be ued later for character instance generation numchars = input("How many characters would you like to create?") #my understanding is that the below call should pass numchars variable CreationSetup(numchars) #everything past this point works correctly and is unimportant to the probem I am having if CCCheck == "no": print("Bite") else: print('Please enter valid input!\n') CharacterCreation() CharacterCreation()
10th Jul 2018, 12:22 PM
Kevin
0
When you pass a value to a function, you have to use it with the very same name you passed it with. def CreationSetup(value): print(value) Note, you never declared 'numchars' variable here in the function, that is why you keep getting an error.
10th Jul 2018, 12:27 PM
strawdog
strawdog - avatar
0
sorry, thats a change i didnt fix, I have tried using numchars instead of value. I undertand that the program is saying "hey i dont see this variable in my local or passed list" i understand the error, i dont understand how it is reaching that conclusion though:-)...the problem in my mind is the logic path defines numchars and then passes it ie: --> run program --> CharacterCreation()--> is yes --> How many characters do you want to create? --> input defines numchars as an integer variable with value = input number (AT THIS POINT numchars ISm DEFINED) --> only path to CharacterSetup is executed passing numchars value to the function -->CharacterSetup shoould print numcharas value with no problems. so why am i having trouble? where is the flaw in my logic my understanding is that where i have numchars = input("how many characters would you like to create?") is the point where i declare and define the variable. is that wrong? thanks for quick reply
10th Jul 2018, 12:37 PM
Kevin
0
Every time you have to keep in mind a SCOPE. The variable defined in one subroutine is visible in the scope of this subroutine. The other subroutines consider it as undefined and raise an error. That is why you should pass it explicitly. bb=0 def dummy(): print(bb) def trueway(value): print(value) def setval(): bb = 3 dummy() trueway(bb) setval() === Output: 0 # <-- see? dummy subroutine does not see the variable set in setval. # It uses 'global' assignment 3 OR you can set a variable as global: bb=0 def dummy(): print(bb) def trueway(value): print(value) def setval(): global bb bb = 3 dummy() trueway(bb) setval() ==== Output: 3 3 ! But i strongly recommend not to use variable globalization
10th Jul 2018, 1:12 PM
strawdog
strawdog - avatar
0
right, so the solution using that line of thought is to run charactercreation in the program itself outside of a method to make it a global variable...the problem is i need to be able to loop it until valid input is entered, ie: yes or no. and numchars is a user input variable that will have no defined vlue until it is assigned by user input during the CharacterCreation function. so i am trying to pass the variable vaue diretly to the charactersetup, which should be possible by just calling the charactersetup function passing numchars as a parameter....I thought that was how variable passing worked...if not should I not instead be able to return numchars to the main program and then pass it to charactersetup as a defined variable from the main program? this is how it should work in my mind, what it seems you are telling me though is that I cannot pass the variable without giving it a definitive value in the program itself...implicitly declaring it...
10th Jul 2018, 1:21 PM
Kevin
0
so let me ask this, I want CharacterCreation to pass numchars to CharacterSetup, I only want to use the value as assigned by the user in CharacterCreation...how do I do this?
10th Jul 2018, 1:22 PM
Kevin
0
Kevin, you can use the canonic way of value passing to a subroutine like this: >>> def subroutine(n=10): print(n) >>> subroutine() 10 >>> subroutine(5) 5 If the user does not assign any value to a variable, it will take 10 as default. Else it will take the varible passed to it. But keep in mind type convertion - input takes a string value, do not forget to convert it to numeric.
10th Jul 2018, 1:29 PM
strawdog
strawdog - avatar
0
scratch, I got what you were saying, I needed to define the CharacterSetup using an parameter so I could then pass the numchars as an argument. sorry had to study the answer you geve a few times before the difference jumped at me.
10th Jul 2018, 1:29 PM
Kevin
0
yep, thats the point of the correct input codes im going to lay in, but since I am just testing the program(this is the first python program I am writing) and I know what it is supposed to do therefore I wont be attempting to break it just yet, I am skipping verification coding to get the program running how it is supposed to then I will go back and drop in all of the antibreak code
10th Jul 2018, 1:31 PM
Kevin
0
now I have one more question if you might be so kind as to answer, the next part of the code in CharacterSetup is goin to be creating a number of instances of a class caled KD = to the value of numchars, I have done some research and I think the best way to code it wil be using a generator and a 'for i in range' code block, but I have tried to C&P the code that I saw that does what I wnt it to and have been unsuccessful in adapting it. any thoughts on how to do this, or a better way to do this?
10th Jul 2018, 1:35 PM
Kevin
0
Kevin, there was a nice solution i saw on SO for class instances counting: from itertools import count class Obj(object): _ids = count(0) def __init__(self): self.id = next(self._ids) Then you can call base class attribure Obj._ids to check the number of instances. Note though, that this is incremention only solution. If you delete an instance it won't affect the counter.
10th Jul 2018, 1:48 PM
strawdog
strawdog - avatar
0
I appreciate it, I will try that and see how it goes! :) thanks!
10th Jul 2018, 1:49 PM
Kevin
0
strawdog, would you mind giving me a break down of what that code does? I want to have the user input numchars, and then have the program create (numchars) instances of the class KD, id also like them to be names KD1, KD2, KD3, etc...I see how this code creates the classes, but I dont see where I would get it to stop at numchars, this looks like it would just keep going. (i should have prefaced with I am way new to python.) thanks
10th Jul 2018, 3:42 PM
Kevin
0
Kevin Well... if i get you right, you might need something like this: from itertools import count class Obj(object): #class definition with instance counter _ids = count(0) def __init__(self): self.id = next(self._ids) def CharacterCreation(): while(True): #infinite loop unti;l user inputs "no" CCCheck = input( "Is this character creation?") if (CCCheck == "yes"): numchars = int(input("How many characters would you like to create?")) objs = [Obj() for i in range(1, numchars+1)] #create 'numchars' instances #of Obj class elif (CCCheck == "no"): print("Bite") break else: print('Please enter valid input!\n') CharacterCreation() print(Obj._ids) === I hope you do really need to use classes (and oop in general), because I personally <blasphemy> tend to avoid oop if it is possible </blasphemy>.
10th Jul 2018, 5:59 PM
strawdog
strawdog - avatar