Hướng dẫn does python3 support raw_input?

As others have indicated, the raw_input function has been renamed to input in Python 3.0, and you really would be better served by a more up-to-date book, but I want to point out that there are better ways to see the output of your script.

From your description, I think you're using Windows, you've saved a .py file and then you're double-clicking on it to run it. The terminal window that pops up closes as soon as your program ends, so you can't see what the result of your program was. To solve this, your book recommends adding a raw_input / input statement to wait until the user presses enter. However, as you've seen, if something goes wrong, such as an error in your program, that statement won't be executed and the window will close without you being able to see what went wrong. You might find it easier to use a command-prompt or IDLE.

Use a command-prompt

When you're looking at the folder window that contains your Python program, hold down shift and right-click anywhere in the white background area of the window. The menu that pops up should contain an entry "Open command window here". [I think this works on Windows Vista and Windows 7.] This will open a command-prompt window that looks something like this:

    Microsoft Windows [Version 6.1.7601]
    Copyright [c] 2009 Microsoft Corporation.  All rights reserved.

    C:\Users\Weeble\My Python Program>_

To run your program, type the following [substituting your script name]:

    python myscript.py

...and press enter. [If you get an error that "python" is not a recognized command, see //showmedo.com/videotutorials/video?name=960000&fromSeriesID=96 ] When your program finishes running, whether it completes successfully or not, the window will remain open and the command-prompt will appear again for you to type another command. If you want to run your program again, you can press the up arrow to recall the previous command you entered and press enter to run it again, rather than having to type out the file name every time.

Use IDLE

IDLE is a simple program editor that comes installed with Python. Among other features it can run your programs in a window. Right-click on your .py file and choose "Edit in IDLE". When your program appears in the editor, press F5 or choose "Run module" from the "Run" menu. Your program will run in a window that stays open after your program ends, and in which you can enter Python commands to run immediately.

Python là một trong những ngôn ngữ dễ học và sử dụng nhất, đồng thời rất mạnh mẽ. Python là một trình thông dịch có mục đích chung, tương tác, hướng đối tượng và cấp cao
ngôn ngữ lập trình.

Nội dung chính

  • Đọc đầu vào từ bàn phím cho Python 2
  • Đọc đầu vào từ bàn phím cho Python 3
  • Cách chuyển đổi giá trị chuỗi thành giá trị nguyên
  • Cách tính diện tích hình tròn

Con trăn input[] và raw_input[]s được sử dụng để đọc dữ liệu từ một đầu vào tiêu chuẩn như bàn phím. Bài viết này tôi sẽ hướng dẫn cách sử dụng hàm raw_input của python trên phiên bản python 2 / python 3 với các ví dụ.

Contents

  • Đọc đầu vào từ bàn phím cho Python 2
  • Đọc đầu vào từ bàn phím cho Python 3
  • Cách chuyển đổi giá trị chuỗi thành giá trị nguyên
  • Cách tính diện tích hình tròn

Đọc đầu vào từ bàn phím cho Python 2

Python 2 có hai phiên bản hàm đầu vào, input[] và raw_input[].

Các input[] xử lý dữ liệu đã nhận dưới dạng chuỗi nếu nó được bao gồm trong dấu ngoặc kép "hoặc" ", nếu không
dữ liệu được coi là số.

    In Python 2

    >>> age = input['How old are you ?']
    How old are you ?: 30 # entered data is treated as number.
    >>> print age
    30

    >>> age = input['How old are you ? :']
    How old are you ? : '30' # entered data is treated as string.
    >>> print age
    '30'

Các input['How old are you ?'] hỏi bạn về tuổi của bạn, Bạn sẽ nhập giá trị số nguyên 30 từ bàn phím, Giá trị này sẽ được lưu trữ trong age dưới dạng số nguyên. Khi chúng tôi in age print ageĐầu ra là giá trị số nguyên 30.

Mặt khác, input['How old are you ?'] hỏi bạn về tuổi của bạn, Bạn sẽ nhập giá trị chuỗi '30' từ bàn phím, Giá trị này sẽ được lưu trữ trong age với kiểu là chuỗi. Khi chúng tôi in age print ageĐầu ra là giá trị chuỗi '30'.

Các raw_input[] xử lý dữ liệu nhận được dưới dạng chuỗi ngay cả khi không có dấu ngoặc kép ”hoặc“ ”.

   In Python 2

   >>> age = raw_input["How old are you ? :"]
   How old are you ? : 30 # entered data is treated as string even without ''
   >>> print age
   '30'

    >>> age = raw_input["How old are you ? :"]
    How old are you ? : '30' # entered data treated as string including ''
    >>> print age
    "'30'"

Các raw_input['How old are you ?'] hỏi bạn về tuổi của bạn, Bạn sẽ nhập giá trị số nguyên 30 hoặc giá trị chuỗi '30' từ bàn phím, Giá trị này sẽ được lưu trữ trong age với kiểu là chuỗi. Khi chúng tôi in age print ageĐầu ra là giá trị chuỗi '30' hoặc "'30 ′".

Đọc đầu vào từ bàn phím cho Python 3

Trong Python 3, raw_input[] không được dùng nữa. Hơn nữa, dữ liệu nhận được luôn
được coi là chuỗi.

    In Python 3

    >>> age = input["How old are you ? :"]
    How old are you ? : 30
    >>> print[age]
    '30'

    >>> age = input["How old are you ? :"]
    How old are you ? : '30' # entered data treated as string with or without ''
    >>> print[age]
    "'30'"

    >>> age = raw_input["How old are you ? :"] # will result NameError
    Traceback [most recent call last]:
    File "", line 1, in 
    NameError: name 'raw_input' is not defined

Các input['How old are you ?'] hỏi bạn về tuổi của bạn, Bạn sẽ nhập giá trị số nguyên 30 hoặc giá trị chuỗi '30' từ bàn phím, Giá trị này sẽ được lưu trữ trong age với kiểu là chuỗi. Khi chúng tôi in age print[age]Đầu ra là giá trị chuỗi '30' hoặc "'30 ′".

Cách chuyển đổi giá trị chuỗi thành giá trị nguyên

Để chuyển đổi giá trị chuỗi thành giá trị số nguyên, chúng tôi sẽ sử dụng int[].

    In Python 2

    >>> age = int[raw_input["How old are you ? :"]]
    How old are you ? : 30
    >>> print[age]
    30

    >>> age = int[raw_input["How old are you ? :"]]
    How old are you ? : '30'
    >>> print[age]
    Traceback [most recent call last]:
    File "", line 1, in 
    ValueError: invalid literal for int[] with base 30: "'30'"
    In Python 3

    >>> age = int[input["How old are you ? :"]]
    How old are you ? : 30
    >>> print[age]
    30

    >>> age = int[input["How old are you ? :"]]
    How old are you ? : '30'
    >>> print[age]
    Traceback [most recent call last]:
    File "", line 1, in 
    ValueError: invalid literal for int[] with base 30: "'30'"

Khi chúng tôi nhập giá trị chữ số, raw_input[] trong python 2 và input[] trong python 3 đã lưu trữ nó dưới dạng giá trị chuỗi. Sau đó int[] chuyển nó thành giá trị số nguyên. Nếu chúng ta nhập giá trị chữ số giữa các dấu ngoặc kép

Cách tính diện tích hình tròn

Chúng tôi sẽ tạo một chương trình đơn giản để đọc dữ liệu nhập từ bàn phím và cách thực thi nó trên Ubuntu 18.04, Tính diện tích hình tròn.

In Python 2

#!/usr/bin/python
import math
radius = int[raw_input["Please enter the radius of the circle : "]]
area_of_circle = math.pi * radius**2
print area_of_circle

Chúng tôi sẽ tạo tệp circle_area2.py và lưu trữ mã trên bên trong nó.

[email protected]:~$ sudo nano circle_area2.py

Để thực hiện chương trình này, Chúng ta sẽ gõ lệnh dưới đây.

[email protected]:~$ sudo python circle_area2.py
    In Python 3

    #!/usr/bin/python3
    import math
    radius = int[input["Please enter the radius of the circle : "]]
    area_of_circle = math.pi * radius**2
    print[area_of_circle]

Chúng tôi sẽ tạo tệp circle_area3.py và lưu trữ mã trên bên trong nó.

[email protected]:~$ sudo nano circle_area3.py

Để thực hiện chương trình này, Chúng ta sẽ gõ lệnh dưới đây.

[email protected]:~$ sudo python circle_area3.py

raw_input[]input[]s trong python 2 và python 3 tương ứng là rất quan trọng để nhận dữ liệu từ người dùng thông qua bàn phím. Ngoài ra chúng ta phải sử dụng int[] để chuyển đổi giá trị chuỗi đầu vào thành giá trị số nguyên.

Chủ Đề