-1

I wanted to write an input to receive a line of text, but it was valid when I typed the newline character directly, and ran the following code. Could the input read the newline character? But it doesn't match the information I found

message=input("please enter your name")
print("your name is",message)
5
  • 1
    your description is confusing, it was valid when I typed the newline character directly, what was valid? also what didn't match and what information you found? Commented Jun 26, 2019 at 7:59
  • Thank you ,I mean the input receives it and continues to run the program,And what I found was that input doesn't read newline character Commented Jun 26, 2019 at 8:08
  • If by ran the following code you mean the print("your name is",message) is executed after you entered an empty line, then I can't reproduce that, and the behavior you encounter is indeed irregular. Commented Jun 26, 2019 at 8:18
  • Thank you very much ,This code is not what I thought, I have corrected it. Commented Jun 26, 2019 at 8:33
  • What is the expected behaviour? Please elaborate in your question. Commented Jun 26, 2019 at 8:40

1 Answer 1

0

First of all, your answer will vary on the basis of the version of python you are using. If using python2.7, instead of 'input' you can use 'raw_input' to take the string as an input from the user. Also, I think you would prefer '+' instead of ',' in print function.

    message = raw_input("please enter your name\n")
    if message:
    print ("your name is"+ message)

If you are using python3 then you can simply add newline character.

    message = input("please enter your name\n")
    if message:
        print ("your name is", message)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much ,I using the python3.7,This code is not what I thought, I have corrected it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.