![]() |
Why can't this code work. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Why can't this code work. (/thread-1936.html) |
Why can't this code work. - sexualpanda - Feb-05-2017 Sorry for not using the correct python terminology I'm new to this. Every time I run this code x=0 def phone(): if brand =='yes': x=x+1 def other(): if x==1: print('it work') brand = input('try this') print(brand) print(x) phone()It does this when the input is yes
RE: Why can't this code work. - micseydel - Feb-05-2017 Python won't let you accidentally change a global variable from a function. There is a way to simply modify the global variable, however it's discouraged generally speaking and very heavily on this particular site. As for what you're really trying to accomplish here... it looks like this is homework. Can you specify the requirements? RE: Why can't this code work. - ichabod801 - Feb-06-2017 Typically you want to do this sort of thing with parameters and return statements: def increment(a): return a + 1 x = increment(x)The value of x is passed as a parameter to the increment function. Internally, increment calls that value 'a'. It adds one to that value and returns it. That become the value of increment(x) in the final line, which is then assigned to x.
|