Problems experienced while incrementing a variable in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

24th Feb 2018, 7:53 PM
Arpan Sircar
Arpan Sircar - avatar
16 Answers
+ 3
just those 3 lines are ok. you can test them in playground. the error must be due to some of your code elsewhere...
24th Feb 2018, 8:00 PM
ifl
ifl - avatar
+ 5
@Racris, please what's the result?
24th Feb 2018, 9:56 PM
📈SmileGoodHope📈
📈SmileGoodHope📈 - avatar
+ 5
Yes,get the value: 2, thanks all, specially to @ifl✔️
24th Feb 2018, 10:08 PM
📈SmileGoodHope📈
📈SmileGoodHope📈 - avatar
+ 4
Using while loop and value of op <2 or more loop will continue: op = 1 while op < 2: print (op) op + = 1
24th Feb 2018, 8:44 PM
📈SmileGoodHope📈
📈SmileGoodHope📈 - avatar
+ 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".
24th Feb 2018, 9:42 PM
📈SmileGoodHope📈
📈SmileGoodHope📈 - avatar
+ 4
print(op) ????
24th Feb 2018, 10:01 PM
📈SmileGoodHope📈
📈SmileGoodHope📈 - avatar
+ 2
Sounds like you run python2.7 with a python3 Programm... Try 'print op' and see if the error persists
24th Feb 2018, 8:00 PM
Chrizzhigh
Chrizzhigh - avatar
+ 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
24th Feb 2018, 8:04 PM
Chrizzhigh
Chrizzhigh - avatar
+ 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
24th Feb 2018, 9:35 PM
Amaras A
Amaras A - avatar
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.
24th Feb 2018, 9:47 PM
Arpan Sircar
Arpan Sircar - avatar
0
@Racris right. Well... I guess we got sidetracked a bit there...
24th Feb 2018, 9:48 PM
Amaras A
Amaras A - avatar
0
Not a problem. Help is always appreciated. :)
24th Feb 2018, 9:51 PM
Arpan Sircar
Arpan Sircar - avatar
0
Result?... um... What are you talking about?
24th Feb 2018, 9:57 PM
Arpan Sircar
Arpan Sircar - avatar
0
The value stored in variable op got incremented by one.
24th Feb 2018, 10:02 PM
Arpan Sircar
Arpan Sircar - avatar
0
well... that was the point of the thingy, wasn't it? (thingy is the technical term for this ^^)
24th Feb 2018, 10:07 PM
Amaras A
Amaras A - avatar
0
Yup.
24th Feb 2018, 10:08 PM
Arpan Sircar
Arpan Sircar - avatar