Python use input to select local variable inside another function - python

All, I have this request but first I will explain what I'm trying to achieve. I coded a python script with many global variables but also many methods defined inside different modules (.py files).
The script sometimes moves to a method and inside this method I call another method defined in another module. The script is quite complex.
Most of my code is inside Try/Except so that every time an exception is triggered my code runs a method called "check_issue()" in which I print to console the traceback and then I ask myself if there's any variable's value I want to double check. Now, I read many stackoverflow useful pages in which users show how to use/select globals(), locals() and eval() to see current global variables and local variables.
What I would specifically need though is the ability to input inside method "check_issue()" the name of a variable that may be defined not as global and not inside the method check_issue() either.
Using classes is not a solution since I would need to change hundreds of lines of code.
These are the links I already read:
Viewing all defined variables
Calling variable defined inside one function from another function
How to get value of variable entered from user input?
This is a sample code that doesn't work:
a = 4
b = "apple"
def func_a():
c = "orange"
...
check_issue()
def check_issue():
print("Something went wrong")
var_to_review = input("Input name of var you want to review")
# I need to be able to enter "c" and print the its value "orange"
print(func_a.locals()[var_to_review ]) # this doesn't work
Could somebody suggest how to fix it?
Many thanks

When you call locals() inside check_issue(), you can only access to the locals of this function, which would be : ['var_to_review'].
You can add a parameter to the check_issue function and pass locals whenever you call it.
a = 4
b = "apple"
def func_a():
c = "orange"
check_issue(locals())
def check_issue(local_vars):
print("Something went wrong")
var_to_review = input("Input name of var you want to review")
print(local_vars[var_to_review])

Related

Passing a variable into a parameter defined by another function python

I am unsure of why the variable totalspeed variable is not being passed correctly to the function startgame as the startgame function is called after the gettotalspeed function.
Exerpt from call function:
gettotalspeed(party_ids)
NoOfEvents=0
startgame(party_ids,totalspeed,distance,NoOfEvents)
Functions
def gettotalspeed(party_ids):
#Get selected party members IDS
print(party_ids)
#Obtain Speeds
ids_string = ','.join(str(id) for id in party_ids)
mycursor.execute("SELECT startspeed FROM characters WHERE CharID IN ({0})".format(ids_string))
myspeeds=mycursor.fetchall()
totalspeed=0
for speedval in myspeeds:
totalspeed=totalspeed + speedval[0]
print("totalspeed is: ",totalspeed)
return totalspeed
def startgame(party_ids,totalspeed,distance,NoOfEvents):
#Check if game end
print(totalspeed)
while distance!=0:
#Travel...
distance=distance-totalspeed
NoOfEvents=NoOfEvents+1
#Generate Random Encounter
genevent(NoOfEvents)
return NoOfEvents
Error Produced:
NameError: name 'totalspeed' is not defined
Outputs (ignoring party_ids)
totalspeed is: 15
I suspect that your problem is self-evident from the main program:
gettotalspeed(party_ids)
NoOfEvents=0
startgame(party_ids,totalspeed,distance,NoOfEvents)
Of the variables you pass to your functions, only NoOfEvents is defined. party_ids, totalspeed, and distance have no definitions.
Work through a tutorial on Python scoping rules. Most of all, note that a function defines a scoping block. Variables inside the function are reclaimed when you leave the function; their names do not apply outside of that block. Your posted program has three independent totalspeed variables.
You forgot to make totalspeed a global variable like global totalspeed in your gettotalspeed() function. You might also be confused about what return does. If you wanted to do it the "proper" way, you could do totalspeed = gettotalspeed(party_ids). Hope this helps!

How To End A Loop When A Condition is Satisfied?

As per my previous question here, my question today is related to it. I have a constantly updating global variable, and I pass that variable to a function. That function consists of a loop and conditional statements. I want the loop to end when the condition is satisfied, but it keeps on looping.
Here is my code.
class LetterAScreen:
def identity(self):
global where
lol=Identifier()
lol.fn_compare()
print where
def verifier(self):
global where
verify=where
if verify != 1:
while (count>0):
print ("try again")
run=LetterAScreen()
run.identity()
run.verifier
print ("try again")
count += 1
else:
print ("correct")
The "Correct" is when the variable turns to one. Otherwise, it is not correct and the user should try again. The output ends up looking like this.
#incorrect inputs
19
try again
try again
19
try again
try again
19
try again
try again
19
try again
try again
19
try again
try again
#correct inputs but loop doesn't end
1
try again
try again
1
try again
try again
1
try again
try again
1
try again
try again
The essential part about Identifier class is only the updating variable. I believe the problem is with the class I've shared. I'm really unfamiliar how this works. I hope you could help me.
There are some things to note here:
Why are your creating a new LetterAScreen object in your loop?
Be carefull when using a global variable, usually there are other ways to solve this cleaner.
I don't know what the Identifier class does but guess the fn_compare function of that class will change the where variable? Also, creating a new Identifier on every call of the identity function seems something you should change.
That being said, the verifier method needs to be adjusted as followed:
def verifier(self):
global where
count = 1
while (count>0 and where != 1):
print ("try again")
self.identity()
count += 1
print ("correct")
You shouldn't assign the global where variable to a local one, this will cause problems because the local one will not be updated in the loop. You can compare to this global where variable directly. Also I removed the run variable, you are executing in an instance of the LetterAScreen class so you can call the identity method on the self object (similar to a this in JS).
first of all you probably don't need to create new instance inside a method "verifier" (methods are already called when the instance is created, you can access the instance using "self" variable.
So instead of
run=LetterAScreen()
run.identity()
write
self.identity()
also instead of of using complex "global" approach
you can create instance variable like self.verify and change it inside "identify"
class LetterAScreen:
def __init__(self):
self.verify = 0
also infinitive loop usually created like this
while True:
Always be careful using "global" is tricky and in most of cases you can do anything in simples and readable way, so I would recommend you remove all globals from this script, you can return params from functions, use mutable objects etc.

Why does a function execute when put inside a variable (and how to stop it)

While writing a program in python i noticed that if one puts a function like print("hello world") inside a variable it will not be stored like expected, instead it will run. Also when i go and call the variable later in the program it will do nothing. can anyone tell me why this is and how to fix it?
If mean something like:
variable = print("hello world")`
then calling the function is the expected result. This syntax means to call the print function and assign the returned value to the variable. It's analogous to:
variable = input("Enter a name")
You're surely not surprised that this calls the input() function and assigns the string that the user entered to the variable.
If you want to store a function, you can use a lambda:
variable = lambda: print("hello world")
Then you can later do:
variable()
and it will print the message

Python: cannot return a value assigned inside of a function outside of that function to later use

def program(n):
name = input("What is your name? >")
return name
print(name)
I have a code that i am trying to execute very similar to this. When executing it, it will not return the variable 'name' i used in the function in order to use that variable outside the function. Why is this?
I am super new to coding by the way so please excuse me if i made a stupid mistake.
When you run your program, you need to assign the result (i.e. whatever is returned in your program, to another variable). Example:
def get_name():
name = input('Name please! ')
return name
name = get_name()
print('Hello ' + name)
Pssst.. I took your function parameter n away since it was not being used for anything. If you're using it inside your actual program, you should keep it :)
For a bit of a more in-depth explanation...
Variables that are declared inside your neat little function over there can't be seen once you come out of it (though there are some exceptions that we don't need to get into right now). If you're interested in how this works, it's known as "variable scope."
To execute the content of a function you need to make a call to the function and assign the return value to some variable. To fix your example, you would do:
def get_name():
name = input("What is your name? >")
return name
name = get_name()
print(name)
I have changed the function name from program() to get_name() seeing as program() is a ambiguous name for a function.
This snippet will make a call to the get_name() function and assign the return value to the variable name. It is important to note, that the name variable inside the function is actually a different variable to the one that we are assigning to outside the function. Note: I have removed the argument n from get_name() since it was not being used.

How can I make 'uName' display the correct name that the user inputs after clarifying checks?

I am very new to Python, as you can probably tell from the code. To begin, I am trying to have the user input their name and store that in a global variable that I can access all throughout my code...preferably named uName.
What's happening is during the loop cycle, it asks the user 'Is this your name?' after they input the first response. If I hit type anything but 'yes' or 'Yes', it will re-ask them to input the name. BUT, when they finally hit 'Yes', the program prints the very first name they entered.
Also, any tips on code structure or wording is helpful...
game.py
from decisions import *
import decisions
global globalname
globalname = ''
def gameEngine(uName):
looper = 0
while looper == 0:
print ('You said your name is, ') + uName + ('...')
clarifier = raw_input('Is that correct?\n')
if clarifier == 'yes' or clarifier == 'Yes':
namePrinter(answer)
else:
decisions.userDecisions(username)
def namePrinter(uName):
print uName
gameEngine(answer)
decisions.py
username = ''
def userDecisions(inputs):
response = raw_input("Please enter your name...\n>>> ")
return response
answer = userDecisions(username)
The specific issue that you are encountering is that you are first running the contents of decisions.py though the import statement in game.py. Through that, you have set the variable "answer" to be equal to the first name that the user inputs.
Then you are calling the gameEngine function in game.py, supplying the "answer" variable from decisions.py as the argument, which is stored in "uName". Upon the user entering another name the name is not stored anywhere and is thrown out with the following line.
decisions.userDecisions(username)
You can assign the return of that statement to a variable such as "uName", and that will get you closer to what you want to do.
uName = decisions.userDecisions(username)
The next issue is that when you are printing out the name, you are printing out the variable "answer" as opposed to "uName". This is what is mainly causing the issue of the first name always being printed out.
namePrinter(answer)
This could be resolved by passing in the "uName" variable instead.
namePrinter(uName)
Also if you want the final chosen name to be stored in the global variable you can assign the final user chosen name to the gloabl variable after the user confirms that the nameis correct.
globalname = uName
However, you may want to be careful about a few parts of the structure of your code.
First, you may want to try not to use global variables. Instead you should be passing around the name though the functions which use it. If you have other player information that you need to access often, you can create a Player class and object to store that information in a single object which can be passed around into functions as needed.
Second, as the userDecisions function does not use its arguement "inputs", you can remove that arguement, as it isn't used.
Third, you may want to be careful about running code through import statements alone. Generally when you are importing a source file, you should be importing the functions, and not rely upon imports to directly run code. For example you can remove the non-function lines of decisions.py and simply run the following in game.py instead.
gameEngine(decisions.userDecisions())
I reccomend that you look up some resources on functions and passing arguement in Python, as they might be able to explain the underlying concepts a bit better.
You have screwed up with the variables and their scope. Read more about them here.
To give you a perspective regarding the scope of variables concisely, look at this code snippet:
# This is a global variable
a = 0
if a == 0:
# This is still a global variable
b = 1
def my_function(c):
# this is a local variable
d = 3
print(c)
print(d)
# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)
# a and b still exist
print(a)
print(b)
# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
Regarding your code, you may want to have a look at these issues:
The answer variable is not accessible in the game.py module.
So is the case with username variable in the decisions.userDecisions(username) call.
The decisions.userDecisions(username) call in the gameEngine(uName) method is not storing the response to any variable and hence the response will be lost.
You are declaring global variable globalname but not assigning any value to it (of course other than '').
P.S.: I was tempted to do your homework for you, but then probably this is good enough information for you to learn more. ;)

Categories

Resources