Code Point
July 31, 2010, 10:58:25 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Geshi Sintax Hilighting Mod Installed. Tens of languages available.
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: I have been having trouble with the print command  (Read 1667 times)
DeathDealer910
Jr. Member
*

Karma: 0
Offline Offline

Posts: 3


« on: July 31, 2009, 12:41:53 PM »

Hello,

i have been directed to the Python programme to allow me to pursue some of my interests in programing and so i went along and downloaded the programme version 3.0.1 and i think i installed it correctly and seems to run fine. so i was also advised to look for some tutorials and start working through them to help me to learn so i went and did so and found one and it was going well i went through a few simple additions and other mathematical operations. then when i got to the print command it came up with an error so i check my typing and tried again again nothing so i copied and pasted from the tutorial into the program the line of code and still the same error. so i wondered whether it was because i had a newer version than that of the one the tutorial was made for so i went and found a new tutorial that i am fairly certain is for 3.0.1 and did the same and then got to printing and nothing so i have copied and pasted directly from my Python IDLE programme to here to see if there is anything wrong with my syntax etc...
>>> print model[0], 'mileage is', mpg[0], 'miles per gallon'
SyntaxError: invalid syntax (<pyshell#69>, line 1)
>>>
 this is the line of code if anyone can point out a mistake of mine that would be great or if you could point out perhaps how i might fix this problem that would also be great thanks

Deathdealer910 (don't be put off by my name its just my gamer name and i get used to using it places makes it easier to use just one name)
Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #1 on: July 31, 2009, 12:56:28 PM »

Python 3.x replaced the python command with the print function:
Code
print ("print is now a function")
 
Logged
DeathDealer910
Jr. Member
*

Karma: 0
Offline Offline

Posts: 3


« Reply #2 on: August 01, 2009, 07:25:06 AM »

Thanks for your help it is much appreciated except it still does not seem to work
Code:
>>> print ("print is now a function")
print is now a function
>>> print "2 + 2 =", 2 + 2
SyntaxError: invalid syntax (<pyshell#3>, line 1)
>>>
that is what i put anymore ideas or did i do something wrong ?
Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #3 on: August 01, 2009, 10:54:22 AM »

Try this:
Code
print ("2 + 2 = %s" % (2 + 2))

Notice that when you call a function its arguments must be passed between parenthesis.

The % is the string substitution operator and %s is where it will be substituted.

I will install Python 3.1 in a virtual machine so I can give better sugestions.
« Last Edit: August 01, 2009, 11:47:11 AM by Alter Lobo » Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #4 on: August 01, 2009, 11:52:54 AM »

The way you wanted to do it also works. Just put the parenthesis:
Code
print ("2 + 2 =", 2 + 2)

The string substitution is much more flexible:
Code
>>> a = 2
>>> b = 3
>>> print ("%s + %s = %s" % (a, b, (a + b)))
2 + 3 = 5

When you start programming you will see this is the kind of thing you will need.
« Last Edit: August 01, 2009, 11:55:06 AM by Alter Lobo » Logged
DeathDealer910
Jr. Member
*

Karma: 0
Offline Offline

Posts: 3


« Reply #5 on: August 02, 2009, 11:42:35 AM »

XD thanks very much for your help its much appreciated

i was a little confused at first by the %s and the % bits but i think i get what they do now ... am i correct in saying that python inserts/substitutes where there is an instance of %s with the objects inside the parenthesis after the symbol % and with multiple instances of %s in the equation being substituted into if there is commas between multiple values in the parenthesis then it substitutes them sequentially ?

and i assume you don't have to enter print ("print is now a function") everytime you print the python program and defiantly not everytime the print command is used (i know the second one is not needed for sure) ... and it seems it makes no difference whethere " or ' is used inside the print command parenthesis at least or is there a difference ... ?

umm this is probably not a private post but hey does not matter that much i guess but yea my real name is actually Myles i just use DeathDealer910 as gamer and username in lots of places to save having to remember lost of them ...
Myles
Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #6 on: August 02, 2009, 12:29:17 PM »

i was a little confused at first by the %s and the % bits but i think i get what they do now ... am i correct in saying that python inserts/substitutes where there is an instance of %s with the objects inside the parenthesis after the symbol % and with multiple instances of %s in the equation being substituted into if there is commas between multiple values in the parenthesis then it substitutes them sequentially ?
You got it.

and i assume you don't have to enter print ("print is now a function") everytime you print the python program and defiantly not everytime the print command is used (i know the second one is not needed for sure)
The print ("print is now a function") was just an example.

... and it seems it makes no difference whethere " or ' is used inside the print command parenthesis at least or is there a difference ... ?
When the value to be printed is a string it must come within double or single quotes. A string is an arbitrary sequence of bytes or characters. A number can't have quotes around it or it will be interpreted as a string. That can be confusing at first but it will not be after a few days/hours of practising.

umm this is probably not a private post but hey does not matter that much i guess but yea my real name is actually Myles i just use DeathDealer910 as gamer and username in lots of places to save having to remember lost of them ...
Myles
Yes that name sounds teenish but for use in a forum it is Ok. Just don't use it in an email list where the tone is more formal.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!