开发者

Print function in python error

I have just started learning python 2.7, and as every new learner i am still getting accustomed to the syntax that python uses. I tried to write this code:

name = raw_input('What is your name?\n')
print 开发者_如何学运维'Hi, %s.' % (name)

I guess the output for the above program should be:

Hi, What is your name?

But i am getting the output as:

What is your name?

After pressing enter key, i get another output:

Hi, .

What is the problem with my code?


There is no problem, it does exactly what you tell it to.

raw_input prints the string argument (as a prompt), and reads input from the user until the first newline, returning the text it has read. This is exactly what happens (try typing some text before hitting Enter). Then the second line takes that text and puts it into the formatting string, printing the result.


The raw_input function is used to take input form the prompt. The string you passed in function will be printed on the prompt followed by your input which gets completed (in sense of python) with the pressing of 'enter key'. After that the next line print 'Hi, %s.' % (name) will get printed showing the name entered by user in the prompt, which, I assume, is None in your case as you are pressing enter key without any input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜