Input and Output: reading from the keyboard and showing results on screen Input and Output: การรับค่าจากคีย์บอร์ดและแสดงผลบนจอ
Learn input() / print() and how to convert a string into a number with int() / float() เรียนรู้ input() / print() และการแปลงสตริงเป็นตัวเลขด้วย int() / float()
Most programs need to take in data from the user and then show a result back.
In Python you read from the keyboard with the input() function, and you show text on the screen
with the print() function. This lesson follows on from Literals and Variables, where you
already met data types and variables.
โปรแกรมส่วนใหญ่ต้องรับข้อมูลจากผู้ใช้แล้วแสดงผลกลับไป
ใน Python การรับค่าจากคีย์บอร์ด (Keyboard) ใช้ฟังก์ชัน input()
และการแสดงผลบนจอ (Screen) ใช้ฟังก์ชัน print()
บทนี้ต่อจาก Literals and Variables ที่รู้จักชนิดข้อมูลและตัวแปรแล้ว
Keyboard and screen Keyboard and Screen
Keyboard (input): input() shows a message on the screen and then waits
until the user types something and presses Enter — the program then keeps that text in a variable.
Screen (output): print() sends text or the value of a variable to the screen.
Keyboard (อินพุต): input() แสดงข้อความบนจอแล้วรอ
จนกว่าผู้ใช้จะพิมพ์แล้วกด Enter — จากนั้นโปรแกรมได้ข้อความนั้นไปเก็บในตัวแปร
Screen (เอาต์พุต): print() ส่งข้อความหรือค่าตัวแปรไปแสดงบนหน้าจอ
1 Show text on screen with print() 1 แสดงผลบนจอด้วย print()
Use print() to send text to the screen (Screen / output). Put the text you want to show inside
quotation marks, "...". Python prints exactly what is inside the quotes, then moves to a new line.
ใช้ print() เพื่อส่งข้อความไปที่หน้าจอ (Screen / output)
ข้อความที่ต้องการแสดงใส่ในเครื่องหมายคำพูด "..."
Python จะพิมพ์ข้อความในเครื่องหมายคำพูดออกมาตรงๆ แล้วขึ้นบรรทัดใหม่
Hello from the screen
2 Read from the keyboard with input() 2 รับค่าจากคีย์บอร์ดด้วย input()
Use input("...") to ask the user to type something — the text in the brackets is the prompt
shown before the program waits for the keyboard.
Very important: input() always returns a string (text), never a number.
Press Run and a box appears for you to type in (for example, type 1). The demo output below shows
one sample run where the user typed 1.
ใช้ input("...") เพื่อถามผู้ใช้ให้พิมพ์อะไรสักอย่าง — ข้อความในวงเล็บคือข้อความที่แสดงก่อนรอคีย์บอร์ด
สำคัญมาก: input() คืนค่าเป็นสตริง (ข้อความ) เสมอ ไม่ใช่ตัวเลข
กด Run แล้วจะมีกล่องให้พิมพ์ (เช่น พิมพ์ 1) ผลลัพธ์ตัวอย่างด้านล่างคือกรณีที่ผู้ใช้พิมพ์ 1
number= 1 1 <class 'str'>
Converting a string into a number การแปลงสตริงเป็นตัวเลข
Because input() always gives you text, you must convert that text into a number
before you can do arithmetic with it. Use these functions:
เพราะ input() ได้มาเป็นข้อความเสมอ ถ้าต้องการนำไปคำนวณทางคณิตศาสตร์
ต้องแปลงข้อความเป็นตัวเลขก่อน โดยใช้ฟังก์ชันต่อไปนี้:
int()— turns text into a whole number, e.g.int("20")gives20float()— turns text into a decimal number, e.g.float("3.5")gives3.5
int()— เปลี่ยนข้อความเป็นจำนวนเต็ม (whole number) เช่นint("20")ได้20float()— เปลี่ยนข้อความเป็นทศนิยม (decimal) เช่นfloat("3.5")ได้3.5
3 Convert a string to an integer with int() 3 แปลงสตริงเป็นจำนวนเต็มด้วย int()
Wrap int() around a piece of text that is a whole number to get an integer value, then use
print() to show the converted value. After conversion you can add, subtract, or multiply it.
ใช้ int() ครอบข้อความที่เป็นตัวเลขทั้งจำนวน เพื่อได้ค่าชนิดจำนวนเต็ม
จากนั้นใช้ print() แสดงค่าที่แปลงแล้ว หลังแปลงแล้วสามารถนำไปบวก ลบ คูณต่อได้
20 25
4 Read a value and convert it to a decimal with float(input()) 4 รับค่าแล้วแปลงเป็นทศนิยมด้วย float(input())
You can wrap float() around input() on one line — the text from the keyboard is
converted to a decimal straight away. If the user types 1, the stored value becomes 1.0
(a decimal). Press Run and type 1 in the input box.
ใช้ float() ครอบ input() ได้ในบรรทัดเดียว — ข้อความจากคีย์บอร์ดจะถูกแปลงเป็นทศนิยมทันที
ถ้าผู้ใช้พิมพ์ 1 ค่าที่พิมพ์จะเป็น 1.0 (ทศนิยม)
กด Run แล้วพิมพ์ 1 ในกล่องรับค่า
number= 1 1.0
5 Example: a personal introduction program 5 ตัวอย่าง: โปรแกรมแนะนำตัว (Personal Introduction)
Ask for a name, a class, and a favourite subject with several input() calls, then use
print() to show all the values on one line — print() puts a space between
each value automatically. Press Run and try typing, for example, Belle → 3-5 → Maths.
เขียนโปรแกรมถามชื่อ ชั้น และวิชาโปรดด้วย input() หลายครั้ง
แล้วใช้ print() แสดงค่าทั้งหมดในบรรทัดเดียว โดยคั่นด้วยช่องว่างอัตโนมัติ
กด Run แล้วลองพิมพ์ เช่น Belle → 3-5 → Maths
Enter your name: Belle Enter your class: 3-5 Enter your favourite subject: Maths Belle 3-5 Maths
Note: Run on this page is a JavaScript simulation, not real Python
(the input() box uses the browser's own prompt window).
For real Python, try
Google Colab.
หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง
(กล่องรับค่า input() ใช้หน้าต่างของเบราว์เซอร์)
หากต้องการทดลอง Python จริง แนะนำ
Google Colab
References References / แหล่งอ้างอิง
Sources used for this lesson:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
-
input()— Built-in Functions — input (reading from the keyboard) (รับค่าจากคีย์บอร์ด) -
print()— Built-in Functions — print (showing output on screen) (แสดงผลบนจอ) -
int()/float()— Built-in Functions (converting text to a number) (แปลงชนิดข้อมูลจากสตริงเป็นตัวเลข) - Built-in Types (str / int / float) — Built-in Types