Quantcast

Jump to content


Photo

How do I start a new line on Python?


  • Please log in to reply
20 replies to this topic

#1 Ozonewolf

Ozonewolf
  • 71 posts

Posted 12 September 2010 - 07:46 PM

I keep pressing enter out of habit. How do I start a new line?

print "Halt!"
user_reply = raw_input("Who goes there? ")
print "You may pass,", user_reply

#2 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 12 September 2010 - 10:07 PM

try "Who goes there? \n"

#3 iargue

iargue
  • 10048 posts


Users Awards

Posted 12 September 2010 - 10:10 PM

You start a new line by pressing enter...

#4 Ozonewolf

Ozonewolf
  • 71 posts

Posted 12 September 2010 - 11:00 PM

Nawwwwww like I didn't try the enter button....

And the /n didn't work either. I'm using Python 2.6 if that helps

#5 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 12 September 2010 - 11:05 PM

ur just trying to add a newline after "who goes there?" right?

#6 Ozonewolf

Ozonewolf
  • 71 posts

Posted 12 September 2010 - 11:09 PM

Yeah. I'm literally just trying to add a new line.

print "Boo"
print "I see you"

Posts as

Boo
I see you

#7 iargue

iargue
  • 10048 posts


Users Awards

Posted 12 September 2010 - 11:24 PM

I really dont see what problem your having...

#8 Ozonewolf

Ozonewolf
  • 71 posts

Posted 12 September 2010 - 11:28 PM

print "Boo"
print "I see you"

DOESN'T come out as

Boo
I see you

I can't get the next print on the line underneath. The best I can do is one string.



#9 iargue

iargue
  • 10048 posts


Users Awards

Posted 12 September 2010 - 11:32 PM

Are you using IDLE?

#10 Ozonewolf

Ozonewolf
  • 71 posts

Posted 12 September 2010 - 11:34 PM

Yes

#11 iargue

iargue
  • 10048 posts


Users Awards

Posted 12 September 2010 - 11:38 PM

Idle uses line by line processing, so you cannot execute two things at once, unless they are part of a function.

To do this in python, type

def printit():
	print "Boo"
	print "I see you"

Then press enter twice.

Then type

printit()

Idle is good for testing out an idea, but for real programming, get an editor such as, Gvim, and then execute it using Python.

#12 Ozonewolf

Ozonewolf
  • 71 posts

Posted 12 September 2010 - 11:41 PM

I'm just starting out for now, thanks i'll try that.

Can you tell me what the 'def' means?

#13 iargue

iargue
  • 10048 posts


Users Awards

Posted 12 September 2010 - 11:43 PM

I'm just starting out for now, thanks i'll try that.

Can you tell me what the 'def' means?



Essentially "define"

It basically creates a function.

When you type Def printit():

You are defining the function printit. Then everything past that point that is indented, is part of that function.

After you decrease the indent, then you leave the function.

#14 Ozonewolf

Ozonewolf
  • 71 posts

Posted 12 September 2010 - 11:45 PM

Nawwww F*CKING H*LL this is soooo f*cking simple and I'm screwing it up (wait can you swear uncensored here??)

I seriously can't get a simple line underneath a line. What do I press? Enter doesn't work, neither does /n or :

It goes

>>> print "blah"
>>> print "blah

and then I can't edit the first print so its a seperate command alltogether. *turns to google*

#15 iargue

iargue
  • 10048 posts


Users Awards

Posted 12 September 2010 - 11:50 PM

Press enter twice and it will unindent and end the function.

#16 Ozonewolf

Ozonewolf
  • 71 posts

Posted 12 September 2010 - 11:54 PM

Nope...ugh im gonna browse google for answers, i sound like a idiot on here. Thanks for trying though, its 99% likely its my fault



#17 Dan

Dan
  • Resident Know-It-All

  • 6382 posts


Users Awards

Posted 13 September 2010 - 01:34 AM


def hello_world:

	print "hello\r\n"

	print "world"



hello_world()


#18 Faval

Faval
  • 637 posts

Posted 13 September 2010 - 07:11 AM

Nawwww F*CKING H*LL this is soooo f*cking simple and I'm screwing it up (wait can you swear uncensored here??)

I seriously can't get a simple line underneath a line. What do I press? Enter doesn't work, neither does /n or :

It goes

>>> print "blah"
>>> print "blah

and then I can't edit the first print so its a seperate command alltogether. *turns to google*


Hmm, I believe someone earlier said to use \n not /n for a newline character in most languages.

Edited by Faval, 13 September 2010 - 07:11 AM.


#19 Ozonewolf

Ozonewolf
  • 71 posts

Posted 13 September 2010 - 07:38 PM

Hmm, I believe someone earlier said to use \n not /n for a newline character in most languages.



tried that too it says 'unexpected characters after liine continuation character'

#20 SmokingKush

SmokingKush
  • 231 posts

Posted 16 September 2010 - 06:28 AM

Strings automatically end with a new line character ( \n ), other than that you can edit the ending character with the (, end = ' ' ) argument. If I understand correctly, your problem is you're entering the code on idle python shell command prompt and not in the editor. Just press Ctrl + N (or file --- new window) and there you'll be able to enter print statements on multiple lines.

Edited by SmokingKush, 16 September 2010 - 06:29 AM.


#21 Pyro699

Pyro699
  • 1543 posts


Users Awards

Posted 06 October 2010 - 02:14 PM

I read most of the posts and kinda got the jist of the problem...


print "Halt!"
user_reply = raw_input("Who goes there?\n")
print "You may pass, ", user_reply


is what your going to want.

Here is the following "line breaks" for the different platforms:
Unix (linux, etc)... \n
Windows \r\n
Mac \r

to use "print" and not have it put a new line out you type:

print "my text without a new line", #note the trailing comma
print "my text with a new line"

import sys
sys.stdout.write("a much faster way to write without a new line")
sys.stdout.write("lets add a new line\n")


Let me know if that clears things up :)
~Cody


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users