Computer Science Canada

Help me!

Author:  AznTiger [ Sun Mar 09, 2008 6:34 pm ]
Post subject:  Help me!

How do you print something, followed by an integer than another string on the same line lets say "you have n butterflys" and n = 10

i.e. in java its System.out.println("you have " + n + "butterflies"); how would you do this in python?

Author:  richcash [ Sun Mar 09, 2008 7:00 pm ]
Post subject:  RE:Help me!

I have never used python (... yet), but you are looking for the print command I think. Shouldn't this work :

code:
print "a", 1, "b"

Author:  AznTiger [ Sun Mar 09, 2008 7:05 pm ]
Post subject:  RE:Help me!

Kay thanks. I've tried everything but that lol and yes it does work

Author:  wtd [ Sun Mar 09, 2008 10:23 pm ]
Post subject:  RE:Help me!

Not as nice as it could be:

Python:
print "You have %d butterflies" % 10

Author:  AznTiger [ Mon Mar 10, 2008 3:17 pm ]
Post subject:  RE:Help me!

alright thanks

Author:  AznTiger [ Mon Mar 10, 2008 6:58 pm ]
Post subject:  RE:Help me!

Now how would you do that for floats instead?

Author:  richcash [ Mon Mar 10, 2008 8:20 pm ]
Post subject:  RE:Help me!

I believe it is %f instead of %d.

Here are some others.

Author:  AznTiger [ Tue Mar 11, 2008 7:54 pm ]
Post subject:  RE:Help me!

and (last question, I promise) how would you do it for many different strings in one line?

Author:  syntax_error [ Tue Mar 11, 2008 10:00 pm ]
Post subject:  Re: Help me!

never used python but based on the above example and google

here
Python:

print "this line of text",
print "will not stop due to the COMMA"


again try that out not 100% sure

btw no need for that to be the last question

Author:  richcash [ Wed Mar 12, 2008 12:42 am ]
Post subject:  Re: RE:Help me!

@syntax_error : I don't think that code should work, you just send the all the objects separated by commas to the print method.

AznTiger @ Tue Mar 11, 2008 7:54 pm wrote:
and (last question, I promise) how would you do it for many different strings in one line?

What do you mean by many strings in one line? The concatenation operator in python is +, as it is in java.
If you are talking about having multiple integers or decimals in a string using the % operator, well I know in ruby you send all arguments in an array :
Ruby:
print "You have %d butterflies and %f nets" % [5, 4.5]

So, that might work in python too.

Author:  McKenzie [ Wed Mar 12, 2008 8:14 am ]
Post subject:  Re: Help me!

syntax_error has the right answer, assuming I understand the question. Obviously his example was not meant to be useful. Still not useful, but a little better is:
code:
for i in range(10):
    print "~",

Author:  wtd [ Wed Mar 12, 2008 10:32 am ]
Post subject:  Re: Help me!

McKenzie @ Wed Mar 12, 2008 9:14 pm wrote:
syntax_error has the right answer, assuming I understand the question. Obviously his example was not meant to be useful. Still not useful, but a little better is:
code:
for i in range(10):
    print "~",


Or better yet...

code:
print "~" * 10

Author:  McKenzie [ Wed Mar 12, 2008 12:00 pm ]
Post subject:  Re: Help me!

The point was not how to make a line. It was why you would ever want to continue outputting on the same line. The only point of my program was to say that he is probably generating solutions in a loop and he wants them displayed on one line.

Author:  richcash [ Wed Mar 12, 2008 2:21 pm ]
Post subject:  RE:Help me!

Ah, my fault, I was beginning to think python's print method was equivalent to ruby's.

syntax_error is correct then, you need a comma at the end.

Author:  wtd [ Wed Mar 12, 2008 4:52 pm ]
Post subject:  RE:Help me!

Alright, it seems I'll have to get more creative to satisfy McKenzie. Wink

code:
print "".join("~" for x in range(10)),


I find that imperative loops like the above to print output are often indicative of a missed opportunity to write idiomatic Python code.

Plus, I just enjoy showing people different ways to do things.

Author:  AznTiger [ Wed Mar 12, 2008 5:19 pm ]
Post subject:  RE:Help me!

Thanks guys, you have no idea how much easier that made life

Author:  AznTiger [ Tue Mar 18, 2008 8:10 pm ]
Post subject:  RE:Help me!

Alright so I know how the comma works now, but everytime i use it in a print command
i.e
print "A", "B"
the result becomes A B instead of AB so does anyone know how to solve this problem?
Thanks again

Author:  wtd [ Tue Mar 18, 2008 8:34 pm ]
Post subject:  RE:Help me!

Don't use "print". Smile

Use the stdout object from the module sys. Specifically the write function. You may also need the flush function is line buffering is present.

Author:  AznTiger [ Tue Mar 18, 2008 9:00 pm ]
Post subject:  RE:Help me!

So how would this work in the actual code?

Author:  wtd [ Wed Mar 19, 2008 9:50 am ]
Post subject:  RE:Help me!

You tell me?

1. How do you import a module?
2. How do you access an object in that module?
3. How do you call a function (method, if you prefer) on an object?


: