简体   繁体   English

Python 中的 raw_input 函数

[英]raw_input function in Python

What is the raw_input function? raw_input函数是什么? Is it a user interface?它是一个用户界面吗? When do we use it?我们什么时候使用它?

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.它向用户显示提示(raw_input([ arg raw_input([arg])的可选参数),从用户那里获取输入并以字符串形式返回用户输入的数据。 See the docs for raw_input() .请参阅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;这与input()的不同之处在于后者试图解释用户给出的输入; it is usually best to avoid input() and to stick with raw_input() and custom parsing/conversion code.通常最好避免使用input()并坚持使用raw_input()和自定义解析/转换代码。

Note: This is for Python 2.x注意:这是针对 Python 2.x 的

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

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

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. raw_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"比方说, a=input(5)将 a 作为值为 5 的整数返回,而a=raw_input(5)将 a 作为字符串“5”返回

The "input" function converts the input you enter as if it were python code. “输入”函数将您输入的输入转换为 python 代码。 "raw_input" doesn't convert the input and takes the input as it is given. “raw_input”不转换输入并接受给定的输入。 Its advisable to use raw_input for everything.建议对所有内容都使用 raw_input。 Usage:用法:

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

The raw_input() function reads a line from input (ie the user) and returns a string raw_input() 函数从输入(即用户)中读取一行并返回一个字符串

Python v3.x as raw_input() was renamed to input()作为 raw_input() 的 Python v3.x 已重命名为 input()

PEP 3111: raw_input() was renamed to input(). PEP 3111:raw_input() 已重命名为 input()。 That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped.也就是说,新的 input() 函数从 sys.stdin 读取一行并返回它,并去除尾随换行符。 It raises EOFError if the input is terminated prematurely.如果输入过早终止,它会引发 EOFError。 To get the old behavior of input(), use eval(input()).要获得 input() 的旧行为,请使用 eval(input())。

Ref: Docs Python 3参考:文档 Python 3

Another example method, to mix the prompt using print, if you need to make your code simpler.另一个示例方法,如果您需要使代码更简单,则可以使用 print 混合提示。

Format:-格式:-

x = raw_input () -- This will return the user input as a string x = raw_input () -- 这将以字符串形式返回用户输入

x= int(raw_input()) -- Gets the input number as a string from raw_input() and then converts it to an integer using int(). 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)

If I let raw_input like that, no Josh or anything else.如果我让 raw_input 那样,没有 Josh 或其他任何东西。 It's a variable,I think,but I don't understand her roll:-( It's a variable,I think,但我不明白她的名字:-(

The raw_input function prompts you for input and returns that as a string. raw_input 函数提示您输入并将其作为字符串返回。 This certainly worked for me.这当然对我有用。 You don't need idle.你不需要闲着。 Just open a "DOS prompt" and run the program.只需打开“DOS 提示符”并运行该程序。

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?"我输入我的名字并在程序打印出“Who Goes there?”后按下[Enter ]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM