0
Problems experienced while incrementing a variable in Python
So, I was trying to increment a variable in Python and experienced few problems. Suppose the variable name is op and the code block is: op = 1 op+=1 print(op) The print statement always yields an error which displays: Traceback (most recent call last): File "<pyshell#51>", line 1, in <module> print(op) TypeError: 'NoneType' object is not callable Could anybody help me with this?
16 Réponses
+ 3
just those 3 lines are ok. you can test them in playground.
the error must be due to some of your code elsewhere...
+ 5
@Racris, please what's the result?
+ 5
Yes,get the value: 2, thanks all, specially to @ifl✔️
+ 4
Using while loop and value of op <2 or more loop will continue:
op = 1
while op < 2:
print (op)
op + = 1
+ 4
@Amaras,value of op is incremental here as per the solution seeker is given ....if value of op has a range then we can stop it by giving range and operate it by " for loop".
+ 4
print(op) ????
+ 2
Sounds like you run python2.7 with a python3 Programm... Try 'print op' and see if the error persists
+ 1
You can also just write 'from __future__ import print_function'... It's a workaround for print in different versions. Now you can use print as function although you in a 2.x Version
+ 1
@Mushfiqur Rahman: the problem in this code is that the incrementation is AFTER the loop, thus the loop variable will never be incremented, thus there will be an infinite loop
0
Alright guys, here's the thing. There's no loop that's supposed to be here. What I wrote was a simple, beginner's code to increment the value stored in the variable. And the code worked with the solution given by @ifl.
0
@Racris right. Well... I guess we got sidetracked a bit there...
0
Not a problem. Help is always appreciated. :)
0
Result?... um... What are you talking about?
0
The value stored in variable op got incremented by one.
0
well... that was the point of the thingy, wasn't it? (thingy is the technical term for this ^^)
0
Yup.