Python Forum
Different outputs in Python2 and Python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Different outputs in Python2 and Python3
#1
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
Reply
#2
(Jun-06-2017, 07:46 PM)MikeHill Wrote:    ...             i+1
That line does nothing, which leads me to believe that it isn't actually the same script you're running with both versions of python.
Reply
#3
(Jun-06-2017, 08:04 PM)nilamo Wrote:
(Jun-06-2017, 07:46 PM)MikeHill Wrote:    ...             i+1
That line does nothing, which leads me to believe that it isn't actually the same script you're running with both versions of python.
It triggers the exception if i is not a number.
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
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ONE input => THREE outputs Tricia279 6 283 Jan-14-2021, 08:52 AM
Last Post: perfringo
  python2 python3 messed up : How to fix ? hary 15 765 Dec-30-2020, 08:26 PM
Last Post: hary
  Multi set string inputs/outputs kwmcgreal 2 363 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 437 Sep-19-2020, 05:46 PM
Last Post: bowlofred
  Getting a small Python2 prog to run in Python3 steve140 4 561 Apr-19-2020, 09:27 AM
Last Post: steve140
  Outputs missing SamAnw 4 585 Feb-12-2020, 04:32 PM
Last Post: adetheheat
  output mismatching when porting a python from python2 env to python3 env prayuktibid 2 690 Jan-21-2020, 04:41 AM
Last Post: prayuktibid
  python3 decoding problem but python2 OK mesbah 0 525 Nov-30-2019, 04:42 PM
Last Post: mesbah
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 1,442 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  Interpreter and running a .py file give different outputs PythonNPC 5 873 Jul-21-2019, 01:07 PM
Last Post: PythonNPC

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020