(Jun-06-2017, 07:46 PM)MikeHill Wrote: Hi All,
I am new to coding
When I run the following code in python2 and python3 I get different outputs
Can someone please explain this phenomenon
>>> a = [1,2,3,4,"hello"] >>> for i in a: ... try: ... print(i) ... i+1 ... print (("i is : %d") %(i)) ... except: ... print ("nope " + i + " is a string") **Python 2 output** 1 2 i is : 1 2 3 i is : 2 3 4 i is : 3 4 5 i is : 4 hello nope hello is a string **Python 3 output** 1 i is : 1 2 i is : 2 3 i is : 3 4 i is : 4 hello nope hello is a string
I get the same output (that looks like your Python2 output) with both versions. I use the plain interpreter for both versions. Obviously your Python3 interactive interpreter doesn't display the result of unassigned expressions (
i+1
) when they happen in the middle of other code. This is possible. If you put your code in a file and run it with either version, you won't get these lines either because displaying the result of unassigned expressions only happens in interactive execution.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Your one-stop place for all your GIMP needs: gimp-forum.net