Python:raw_input and input usage

Source: Internet
Author: User

Input and raw_input can be used to read the console inputs, but input and raw_input are different when working with numbers
Pure Digital input

When the input is a pure number

Input returns a numeric type, such as Int,float
Raw_inpout return String type, String type

Input string is an expression

Input computes the numeric expression in the string, and Raw_input does not.

If you enter "57 + 3":

Input will get an integer 60
Raw_input will get the string "57 + 3"

Implementation of Python input

Look at the Python input document, you can see that input is actually implemented through raw_input, the principle is very simple, the following line of code:

def input (prompt):
Return (eval (raw_input (prompt)))

Python raw_input Read Input value

The following describes the use of Python raw_input,
The raw_input can be used to read data in a very convenient bundle console.
1. Input string

#13222319810101 * * *
NID = "
While 1:
NID = raw_input ("Input your id plz")
If Len (nID)! = Len ("13222319810101****"):
print ' wring length of id,input again '
Else
Break

print ' Your ID is%s '% (NID)

2. Enter an integer

nAge = Int (raw_input ("Input your Age plz:\n")
If NAge > 0 and NAge < 120:
print ' thanks! '
Else
print ' bad age '
print ' Your age is%d\n '% nAge

3. Enter floating-point type

Fweight = 0.0
Fweight = float (raw_input ("Input your weight\n"))
print ' Your weight is%f '% fweight

4. Enter 16 binary data

Nhex = Int (raw_input (' Input hex value (like 0x20): \ n '), 16)
print ' Nhex =%x,noct =%d\n '% (Nhex,nhex)

5. Enter 8 binary data

noct = Int (raw_input (' Input Oct value (like 020): \ n '), 8)
print ' noct =%o,ndec =%d\n '% (noct,noct)

Reprint: http://blog.csdn.net/kjing/article/details/7450146

Python:raw_input and input usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.