I am trying to change variable (state) from fun3 which is called from multiple function as shown in the image.
When i print state from inside fun3, it prints the modified variable game.But when i print state from func1, it prints menu.
Problem: it is not modifying the orignal variable in the file game.py.
I spent 3 hours to solve the problem but it is not solving.
If any one has the solution Please help in this problem.
Related
I often find myself in a situation where i need to check if i assigned something to a variable and then if so, compare it's value to something else.
Sure, I know how to do it, but I wonder if there is a more elegant way of handling this.
EXAMPLE:
def function():
with suppress(SomeException):
variable = 10
if variable and variable > 5:
# do things
That will give me an UnboundLocalError exception (in case of SomeException happening during variable assignment). Again, I know how to code it, so it will work, it's the style of coding that troubles me. I want to avoid creating unnecessary local variable.
You say you know how to do it, but the code you posted will throw an exception if the assignment doesn't happen.
There really is a simple way to do this: just don't write code that can leave a variable uninitialised.
def function():
variable = None
if SomeCondition():
variable = 10
if variable is not None:
# do things
Note that this doesn't create any unneeded local variables, the variable variable would be created whether or not you assign to it, but if you don't assign then it is uninitialised and testing whether it is initialised means having to throw and catch an exception, and that is slow. Simply initialising it means you have a simple easy test. Stop trying to optimise things that don't need optimising.
I have a big dictionary and some of the elements occasionally end up with illegal values. I want to figure out where the illegal values are coming from. PyCharm should constantly monitor the values of my dictionary, and the moment any of them take the illegal value, it should break and let me inspect the state of the program.
I know I can do this by just creating a getter/setter for my dictionary instead of accessing it directly, and then break inside the setter with an appropriate condition.
Is there a way to do it without modifying my code?
I'm not sure if this answers your question but you can set a breakpoint on the line of code you want to break at, right click on that break point once it is set and then apply a condition.
An example of such a condition could be:
x > 5
Once you are at the stage in your loop/code where this condition is true i.e. when x = 6 then it will break and you can inspect all the current values/ status of your code.
Hope this helps
Using python 2.7x:
I've searched through other questions but could not find anything similar to my functions. I'm writing to an excel spreadsheet. The variable a is not used anywhere else in the whole program. I have tried putting the global variable before the function, inside the function, adding into the function as shown below, changing from for loop to while loop, etc, almost everything as suggested by other posts, but they do not work unfortunately.
Could it be that since I'm using an array, it is affecting the code too?
I keep getting IndexError: list index out of range for this while loop and have no idea how to solve. Thanks so much for your help!
GPA is an array somewhere else in the program.
def function(a):
while (a <785):
sheet1.write(a+30,10,GPA[a][0])
sheet1.write(a+31,10,GPA[a][0])
sheet1.write(a+30,11,GPA[a][1])
sheet1.write(a+31,11,GPA[a][1])
sheet1.write(a+30,12,GPA[a][2])
sheet1.write(a+31,12,GPA[a][2])
a = a+2
function (1)
The codes are correct. The problem lies with the array which I've solved it.
I have an aged code from different authors who use global variables over many years and the following challenge: I imported several (>200) variables from an excel file. They are not inside a function and they are not designated as global. For the function to work, they have to be because there is one function1 which calls a function 2. Function 1 is called once only while function 2 is called >10000 times, so I wish anything global to be inside function 1. How can I easily turn all of them into global variables and pass them to function 2?
example (scheme)
% function1
% global *other variables exist here already*
% this function calls function2 at a certain point further below
L=whos
save L % some variables a b c ... are parameters in function2
% function2
% global *other variables exist here already*
load L % i dont want to load my workspace everytime, I rather wish to just access global variables a b c...
% the problem is that my variables sometimes change name and I want to have
% them all global in an automatic way. Or live with a workaround.
Thank you for your patience and I am ready to take questions!
UPDATE I:
I managed a workaround with which im not entirely happy because it involves manual manipulation in the code. So it is a temporary solution at best.
% lets say you have some workspace variables and you save them under this name, and then load them:
load workspacevars.mat;
L=who % gives you those variable's names as string column
L=L' % and in a form that makes you able to use it as global (as row)
% unfortunately using global L doesnt work for me. any ideas?
% I had to go to L in the workspace, and click and drag the resulting long string into a text editor.
% there, I removed the braces {} and the '' because this is how global likes to have their variables: pure.
% finally it looks like this: global var1 var2 var3 ....
The answer is, use structure files instead of globals.
once the struct is created, (c.constant1, c.constant2...), you just give the function in question c. I used to work with workspace a lot, which explains why I save my workspace and load it once (not many times since constants dont change) so this is how it looks for me:
mainfunction
c.constant1=1;
save c.mat
[Time,Results] = ode15s(#(x, c) f1(x, c),[0 c.length],x0,options)
end
function [OUTPUT] = f1(t, x, c)
load c.mat
end
inputs are x and c and they can be freely shared between both the main and the subfunction. If constants should change due to some events in the function, they may be saved with an if conditions inside the subfunction and are reloaded in the next iteration of the code (but this is another issue adressed here: Change a constant in ODE calculations under particular conditions with a flag)
this is my first time asking a question so let me know if I am doing something wrong (post wise)
I am trying to create a function that writes into a .txt but i seem to get two very different results between calling it from within a module, and writing the same loop in the shell directly. The code is as follows:
def function(para1, para2): #para1 is a string that i am searching for within para2. para2 is a list of strings
with open("str" + para1 +".txt", 'a'. encoding = 'utf-8') as file:
#opens a file with certain naming convention
n = 0
for word in para2:
if word == para1:
file.write(para2[n-1]+'\n')
print(para2[n-1]) #intentionally included as part of debugging
n+=1
function("targetstr". targettext)
#target str is the phrase I am looking for, targettext is the tokenized text I am
#looking through. this is in the form of a list of strings, that is the output of
#another function, and has already been 'declared' as a variable
when I define this function in the shell, I get the correct words appearing. However, when i call this same function through a module(in the shell), nothing appears in the shell, and the text file shows a bunch of numbers (eg: 's93161), and no new lines.
I have even gone to the extent of including a print statement right after declaration of the function in the module, and commented everything but the print statement, and yet nothing appears in the shell when I call it. However, the numbers still appear in the text file.
I am guessing that there is a problem with how I have defined the parameters or how i cam inputting the parameters when I call the function.
As a reference, here is the desired output:
‘She
Ashley
there
Kitty
Coates
‘Let
let
that
PS: Sorry if this is not very clear as I have very limited knowledge on speaking python
I have found the solution to issue. Turns out that I need to close the shell and restart everything before the compiler recognizes the changes made to the function in the module. Thanks to those who took a look at the issue, and those who tried to help.