Python中的raw_input函数

问题:Python中的raw_input函数

功能是什么raw_input?是用户界面吗?我们什么时候使用它?

What is the raw_input function? Is it a user interface? When do we use it?


回答 0

它提出了一个提示给用户(可选argraw_input([arg])),从用户获得输入,并通过在一个字符串的用户返回数据的输入。请参阅有关的文档raw_input()

例:

name = raw_input("What is your name? ")
print "Hello, %s." % name

这与input() 后者的不同之处在于后者试图解释用户给出的输入。通常最好避免input()并坚持使用raw_input()自定义的解析/转换代码。

注意:这适用于Python 2.x

It presents a prompt to the user (the optional arg of raw_input([arg])), gets input from the user and returns the data input by the user in a string. See the docs for raw_input().

Example:

name = raw_input("What is your name? ")
print "Hello, %s." % name

This differs from input() in that the latter tries to interpret the input given by the user; it is usually best to avoid input() and to stick with raw_input() and custom parsing/conversion code.

Note: This is for Python 2.x


回答 1

raw_input()input()在Python 3中被重命名为。

来自http://docs.python.org/dev/py3k/whatsnew/3.0.html

raw_input() was renamed to input() in Python 3.

From http://docs.python.org/dev/py3k/whatsnew/3.0.html


回答 2

“输入”功能将您输入的输入转换为Python代码。“ raw_input”不会转换输入,而是按照给定的方式接受输入。建议对所有内容使用raw_input。用法:

>>a = raw_input()
>>5
>>a
>>'5'

The “input” function converts the input you enter as if it were python code. “raw_input” doesn’t convert the input and takes the input as it is given. Its advisable to use raw_input for everything. Usage:

>>a = raw_input()
>>5
>>a
>>'5'

回答 3

raw_input是一种输入形式,采用字符串形式的参数,而input函数则根据您的输入来采用值。假设a = input(5)返回a作为值为5的整数,而a = raw_input(5)返回a作为字符串“ 5”

raw_input is a form of input that takes the argument in the form of a string whereas the input function takes the value depending upon your input. Say, a=input(5) returns a as an integer with value 5 whereas a=raw_input(5) returns a as a string of “5”


回答 4

如果需要使代码更简单,则另一个示例方法是使用print混合提示。

格式:-

x = raw_input()-这将以字符串形式返回用户输入

x = int(raw_input())-从raw_input()以字符串形式获取输入数字,然后使用int()将其转换为整数。

print '\nWhat\'s your name ?', 
name = raw_input('--> ')
print '\nHow old are you, %s?' % name,
age = int(raw_input())
print '\nHow tall are you (in cms), %s?' % name,
height = int(raw_input())
print '\nHow much do you weigh (in kgs), %s?' % name,
weight = int(raw_input())

print '\nSo, %s is %d years old, %d cms tall and weighs %d kgs.\n' %(
name, age, height, weight)

Another example method, to mix the prompt using print, if you need to make your code simpler.

Format:-

x = raw_input () — This will return the user input as a string

x= int(raw_input()) — Gets the input number as a string from raw_input() and then converts it to an integer using int().

print '\nWhat\'s your name ?', 
name = raw_input('--> ')
print '\nHow old are you, %s?' % name,
age = int(raw_input())
print '\nHow tall are you (in cms), %s?' % name,
height = int(raw_input())
print '\nHow much do you weigh (in kgs), %s?' % name,
weight = int(raw_input())

print '\nSo, %s is %d years old, %d cms tall and weighs %d kgs.\n' %(
name, age, height, weight)

回答 5

如果我让raw_input这样,就不要Josh或其他任何东西。我想这是一个变量,但我不理解她的角色:-(

raw_input函数提示您输入,并以字符串形式返回。这肯定对我有用。您不需要闲置。只需打开“ DOS提示符”并运行程序。

这就是我的样子:

C:\temp>type test.py
print "Halt!"
s = raw_input("Who Goes there? ")
print "You may pass,", s

C:\temp>python test.py
Halt!
Who Goes there? Magnus
You may pass, Magnus

[Enter在程序打印“谁去那里?”之后,我键入我的名字并按]。

If I let raw_input like that, no Josh or anything else. It’s a variable,I think,but I don’t understand her roll :-(

The raw_input function prompts you for input and returns that as a string. This certainly worked for me. You don’t need idle. Just open a “DOS prompt” and run the program.

This is what it looked like for me:

C:\temp>type test.py
print "Halt!"
s = raw_input("Who Goes there? ")
print "You may pass,", s

C:\temp>python test.py
Halt!
Who Goes there? Magnus
You may pass, Magnus

I types my name and pressed [Enter] after the program had printed “Who Goes there?”