Python Forum

Full Version: Print Not Printing Correctly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
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)) 
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