Reputation: 11
This is a minimal representation of my original code
x = 0
count = 0
a=30-count
while x < 10:
if True:
count = count+1
x = x+1
print("count is ",count)
print("a is ",a)
This is the output:
count is 10
a is 30
The a in the output was supposed to be 20 but the line 'a=30-count' is not working
To make it work I have to move the line 'a=30-count' just above the 'print("a is ",a)' but I want the line 'a=30-count' in the beginning of the code
I have tried setting a as a global variable but that didn't worked
Thank you in advance also sorry if the format of my question wasn't right its my first time posting a question on this site
Upvotes: 0
Views: 985
Reputation: 15505
When you write a = 30 - count
, it doesn't "bind" variable a
to variable count
. It simply calculates a value for a
, using the current value for count
.
If you want to "save" the expression 30 - count
to be reevaluated later, you can put it in a function:
def a(count):
return 30 - count
x = 0
count = 0
while x < 10:
count = count+1
x = x+1
print("count is ",count)
print("a is ",a(count))
# count is 10
# a is 20
Upvotes: 1
Reputation: 154
x = 0
count = 0
a=30-count
while x < 10:
if True:
count = count+1
x = x+1
print("count is ",count)
print("a is ",a)
is equavalent to
x = 0
count = 0
a=30-count
while x < 10:
count = count + 1
x = x + 1
print("count is ",count)
print("a is ",a)
Discussing the problem , python evaluates the code form top to bottom. So as the time you set a = 30-count the value of count is 0 so 'a' gets the value 30. As you said the most significant way to solve this problem is by putting line before the print statement or create a function
def updateA():
global a
a = 30 - count
and call the function updateA() before the print statement. I hope this will work
Upvotes: 1
Reputation: 419
This is not working because you declared the value of a before you changed the value of count, So a has the value : 30 - 0 which is nothing but 30.
After running through the WHILE loop, the value of count changes to 10, but the value of a is not updated because it has already been declared as (30-0) earlier.
The Variable a will not keep updating every time the value of count changes, that's why you have to update it's value again.
here's the code :
count = 0
a = 30
for x in range(10):
count += 1
a -= count
print("count is ",count)
print("a is ",a)
The Output should look like this :
count is 10
a is 20
Upvotes: 1
Reputation:
Python will interpret count
as 0 and a
= 30-0 hence the answer. And then it will proceed to the while loop
.
You while have to move the line a=30-count
after the while loop
.
x = 0
count = 0
while x < 10:
count +=1
x = x+1
a=30-count
print("count is",count)
print("a is",a)
Upvotes: 1
Reputation: 41
a=30-count
is only called once when you initially call it. To rectify this you should call it at the end of the while loop.
x = 0
count = 0
while x < 10:
if True:
count = count+1
x = x+1
a=30-count
this gives you
count is 10
a is 20
Also
if True:
is redundant as your while loop is essentially saying while x < 10 is true continue.
Upvotes: 1
Reputation: 508
Python is a procedural imperative programming language and the code is run from top to bottom. When you put a=30-count
at the beginning of the code, python will interpret it as a = 30 - 0
by replacing count = 0 to that line of code. So, it prints 30.
Try moving print("a is ",a)
to the line right below a=30-count
and you will see that a is 30.
You already know the correct way to make it works, which is to move a=30-count
just above print("a is ", a)
because then python is replaying count = 10 there.
Upvotes: 1