Python Forum
Print Not Printing Correctly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print Not Printing Correctly
#1
I'm in a Python Programming course right now, and I know there is a classroom forum, but this isn't about an assignment, rather about the format not working properly only for the students using Mac OS (myself included).

So, if we type:
numbers = [ ]
strings = [ ]
names = ["John", "Eric", "Jessica"]

numbers.append(1)
numbers.append(2)
numbers.append(3)

strings.append('hello')
strings.append('world')

second_name = names[1]

print(numbers)
print(strings)
print(names)

print(names[1])
print(names[2])
print(strings[0] + " " + strings[1])
print(strings[0] + " " + names[2])

In the console it shows:
[1, 2, 3]
['hello', 'world']
['John', 'Eric', 'Jessica']
Eric
Jessica
hello world
hello Jessica


Now with all of that typed in, here's where the format gets messed up.
When we try to have Python print strings and numbers, it no longer shows up properly on the console.

Typing this:
print(strings[0], numbers[1])

Produces this:
('hello', 2)

Now, if we remove the parenthesis and instead type:
print strings[0], numbers[1]

Then, we get what we actually want in the console:
hello 2

There's obviously an error code that is then received, since the print format is supposed to have the parenthesis.

This also happened when we did the code with somewhat different variables:
myfloat = 10.0
myint = 20
sumNumbers = myfloat + myint

mystring = "hello"
Saying = "Saying"
t = "times"
mystring2 = "will give you"

print(Saying + " " + mystring)
print(myint)
print(myfloat)
print(mystring2)

print(Saying + " " + mystring, myint, t, myfloat, mystring2, sumNumbers)
print Saying + " " + mystring, myint, t, myfloat, mystring2, sumNumbers

s = 'Saying' + " " + 'hello'
t = 'will give you'
u = 'times'
print (s, myint, u, myfloat, t, sumNumbers)
print s, myint, u, myfloat, t, sumNumbers

These prints produce:
Saying hello
20
10.0
will give you
('Saying hello', 20, 'times', 10.0, 'will give you', 30.0)
Saying hello 20 times 10.0 will give you 30.0
('Saying hello', 20, 'times', 10.0, 'will give you', 30.0)
Saying hello 20 times 10.0 will give you 30.0

So, to summarize the issue, when separating words and numbers, print works perfectly fine with what we want to appear in the console, but when we use the format to combine the two (like we're supposed to), then we have the quotations, commas, and parenthesis showing up.

We all have the latest Mac OS X, along with Python 3 and Eclipse Photon (with the IDE for Java Developers, and PyDev).

Any help would be extremely useful. Thank you in advance and have a wonderful day.
Reply
#2
No, you don't all have Python 3. The behavior you are describing is the behavior of the print statement in Python 2 as opposed to the print function in Python 3.

Edit: You may all have Python 3, but the Mac may have Python 2 on it as well, and you need to explicitly use Python 3.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Learn how to format output
print (s, myint, u, myfloat, t, sumNumbers)
## becomes
print("%s %d %s %5.1f %s %5.1f" % (s, myint, u, myfloat, t, sumNumbers)) 
Reply
#4
To properly install Python 3 (you should use version 3.7) follow Snippsats tutorials here:
if windows:
Python and pip installation under windows Part 1
and
Python and pip installation under windows Part 2
or for Linux:
https://python-forum.io/Thread-Part-1-Li...nvironment
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  CSV not printing apostrophe correctly bazcurtis 8 2,477 Aug-22-2022, 04:28 PM
Last Post: bazcurtis
  print not printing newlines rocket777 4 2,301 Jul-03-2021, 09:43 PM
Last Post: bowlofred
  printing a list contents without brackets in a print statement paracelx 1 2,140 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  Printing output without print usage susmith552 1 1,672 Feb-07-2020, 07:52 PM
Last Post: Clunk_Head
  How to print the map correctly janaki26794 0 1,379 Aug-06-2019, 11:00 PM
Last Post: janaki26794

Forum Jump:

User Panel Messages

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