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
  format json outputs ! evilcode1 3 1,692 Oct-29-2023, 01:30 PM
Last Post: omemoe277
Sad Migrating of python2 script to python3 zuri 7 870 Oct-05-2023, 02:40 PM
Last Post: snippsat
  Migration of Python2 and Python3 using Modernize and Future Rakshan 5 2,456 Oct-05-2023, 08:55 AM
Last Post: zuri
  Formatting outputs created with .join command klairel 2 594 Aug-23-2023, 08:52 AM
Last Post: perfringo
  I have written a program that outputs data based on GPS signal kalle 1 1,134 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  Why does absence of print command outputs quotes in function? Mark17 2 1,342 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,449 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Combining outputs into a dataframe rybina 0 1,649 Mar-15-2021, 02:43 PM
Last Post: rybina
  ONE input => THREE outputs Tricia279 6 2,561 Jan-14-2021, 08:52 AM
Last Post: perfringo
  python2 python3 messed up : How to fix ? hary 15 7,841 Dec-30-2020, 08:26 PM
Last Post: hary

Forum Jump:

User Panel Messages

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