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 จะพิมพ์ข้อความในเครื่องหมายคำพูดออกมาตรงๆ แล้วขึ้นบรรทัดใหม่

[1]
[output]
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

[2]
[output]
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") gives 20
  • float() — turns text into a decimal number, e.g. float("3.5") gives 3.5
  • int() — เปลี่ยนข้อความเป็นจำนวนเต็ม (whole number) เช่น int("20") ได้ 20
  • float() — เปลี่ยนข้อความเป็นทศนิยม (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() แสดงค่าที่แปลงแล้ว หลังแปลงแล้วสามารถนำไปบวก ลบ คูณต่อได้

[3]
[output]
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 ในกล่องรับค่า

[4]
[output]
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

[5]
[output]
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:

แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:

  1. input() — Built-in Functions — input (reading from the keyboard) (รับค่าจากคีย์บอร์ด)
  2. print() — Built-in Functions — print (showing output on screen) (แสดงผลบนจอ)
  3. int() / float() — Built-in Functions (converting text to a number) (แปลงชนิดข้อมูลจากสตริงเป็นตัวเลข)
  4. Built-in Types (str / int / float) — Built-in Types

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

Which function reads a value from the keyboard?ฟังก์ชันใดใช้รับค่าจากคีย์บอร์ด (Keyboard)?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) input()input()

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) input()input()
    Correct! input() reads the text the user types on the keyboard.ถูกต้อง! input() ใช้รับข้อความที่ผู้ใช้พิมพ์จากคีย์บอร์ด
  • (B) print()print()
    Wrong: print() shows output on screen, it does not read the keyboard.ผิด: print() ใช้แสดงผลบนจอ ไม่ใช่รับค่าจากคีย์บอร์ด
  • (C) int()int()
    Wrong: int() converts text to a whole number, it does not read the keyboard.ผิด: int() ใช้แปลงข้อความเป็นจำนวนเต็ม ไม่ใช่รับค่าจากคีย์บอร์ด
  • (D) float()float()
    Wrong: float() converts text to a decimal, it does not read the keyboard.ผิด: float() ใช้แปลงข้อความเป็นทศนิยม ไม่ใช่รับค่าจากคีย์บอร์ด

ข้อ 2

2 / 10

What type does input() always return?input() คืนค่าชนิดใดเสมอ?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) string (str)สตริง (str)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) integer (int)จำนวนเต็ม (int)
    Wrong: even if the user types digits, input() still returns a string.ผิด: แม้ผู้ใช้พิมพ์ตัวเลข input() ก็ยังคืนค่าเป็นสตริง
  • (B) string (str)สตริง (str)
    Correct! input() always returns a string (text).ถูกต้อง! input() คืนค่าเป็นสตริง (ข้อความ) เสมอ
  • (C) decimal (float)ทศนิยม (float)
    Wrong: you must wrap float() around input() to get a decimal.ผิด: ต้องใช้ float() ครอบ input() ถ้าต้องการทศนิยม
  • (D) boolean (bool)บูลีน (bool)
    Wrong: input() does not return a boolean.ผิด: input() ไม่คืนค่าเป็นบูลีน

ข้อ 3

3 / 10

Which function converts text into a whole number?ฟังก์ชันใดแปลงข้อความเป็นจำนวนเต็ม?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) int()int()

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) print()print()
    Wrong: print() shows output, it does not convert types.ผิด: print() ใช้แสดงผล ไม่แปลงชนิดข้อมูล
  • (B) input()input()
    Wrong: input() reads a string, it does not convert to an integer.ผิด: input() รับค่าเป็นสตริง ไม่ได้แปลงเป็นจำนวนเต็ม
  • (C) int()int()
    Correct! int() turns text into a whole number, e.g. int("20") gives 20.ถูกต้อง! int() เปลี่ยนข้อความเป็นจำนวนเต็ม เช่น int("20") ได้ 20
  • (D) float()float()
    Wrong: float() converts to a decimal, not a whole number.ผิด: float() แปลงเป็นทศนิยม ไม่ใช่จำนวนเต็ม

ข้อ 4

4 / 10

After number = float(input(...)) and the user types 1, what does print(number) show?หลังรัน number = float(input(...)) ผู้ใช้พิมพ์ 1 แล้ว print(number) ได้ค่าใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) 1.01.0

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 11
    Wrong: the value was converted to a decimal, so it shows as 1.0.ผิด: ค่าถูกแปลงเป็นทศนิยมแล้ว จึงแสดงเป็น 1.0
  • (B) "1""1"
    Wrong: float() converted the string to a number, it is no longer a string.ผิด: float() แปลงสตริงเป็นตัวเลขแล้ว ไม่เหลือเป็นสตริง
  • (C) ErrorError
    Wrong: typing 1 and converting with float() works fine.ผิด: พิมพ์ 1 แล้วแปลงด้วย float() ได้ปกติ
  • (D) 1.01.0
    Correct! float() turns 1 into the decimal 1.0, as shown in the lesson.ถูกต้อง! float() ทำให้ 1 กลายเป็นทศนิยม 1.0 ตามตัวอย่างในบท

ข้อ 5

5 / 10

Which line prints the name, class, and subject on one line, as in the introduction program?บรรทัดใดแสดงชื่อ ชั้น และวิชาในบรรทัดเดียวตามตัวอย่างโปรแกรมแนะนำตัว?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) print(name, c, fs)print(name, c, fs)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) print(name, c, fs)print(name, c, fs)
    Correct! print() takes several comma-separated values and shows them on one line separated by spaces.ถูกต้อง! print() รับหลายค่าคั่นด้วยจุลภาค จะแสดงในบรรทัดเดียวคั่นด้วยช่องว่าง
  • (B) input(name, c, fs)input(name, c, fs)
    Wrong: input() reads a value, it does not print several variables like this.ผิด: input() ใช้รับค่า ไม่ใช่แสดงผลหลายตัวแปรพร้อมกันแบบนี้
  • (C) int(name, c, fs)int(name, c, fs)
    Wrong: int() converts to a whole number, it does not print.ผิด: int() ใช้แปลงเป็นจำนวนเต็ม ไม่ใช่แสดงผล
  • (D) float(name, c, fs)float(name, c, fs)
    Wrong: float() converts to a decimal, it does not print.ผิด: float() ใช้แปลงเป็นทศนิยม ไม่ใช่แสดงผล

ข้อ 6

6 / 10

After text = input("n= ") and the user types 5, what is type(text)?text = input("n= ") แล้วผู้ใช้พิมพ์ 5 ค่า type(text) คืออะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) <class 'str'><class 'str'>

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) <class 'int'><class 'int'>
    Wrong: input() does not convert the type; the value is still a string.ผิด: input() ไม่แปลงชนิดให้ ค่าที่ได้ยังเป็นสตริง
  • (B) <class 'str'><class 'str'>
    Correct! Even when digits are typed, the value from input() is a str.ถูกต้อง! ต่อให้พิมพ์ตัวเลข ค่าจาก input() ก็เป็น str
  • (C) <class 'float'><class 'float'>
    Wrong: you would need float() around it first to get a float.ผิด: ต้องใช้ float() ครอบก่อนจึงจะเป็น float
  • (D) <class 'bool'><class 'bool'>
    Wrong: input() does not return a boolean.ผิด: input() ไม่คืนค่าบูลีน

ข้อ 7

7 / 10

To add two numbers read with input(), what should you do?ถ้าต้องบวกเลขจาก input() สองค่า ควรทำอย่างไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) Convert each value with int() or float() before addingแปลงแต่ละค่าด้วย int() หรือ float() ก่อนบวก

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Add the strings directly, e.g. input() + input()บวกสตริงตรงๆ เช่น input() + input()
    Wrong: adding strings joins the text, e.g. "2"+"3" gives "23".ผิด: การบวกสตริงคือการต่อข้อความ เช่น "2"+"3" ได้ "23"
  • (B) Wrap input() in print() before addingใช้ print() ครอบ input() ก่อนบวก
    Wrong: print() shows output, it does not convert to a number.ผิด: print() แสดงผล ไม่แปลงเป็นตัวเลข
  • (C) Python adds them as numbers automaticallyPython บวกให้อัตโนมัติเป็นตัวเลข
    Wrong: Python does not guess the type; input() values stay strings.ผิด: Python ไม่เดาชนิดให้ ค่าจาก input() ยังเป็นสตริง
  • (D) Convert each value with int() or float() before addingแปลงแต่ละค่าด้วย int() หรือ float() ก่อนบวก
    Correct! Convert the text to numbers first, then the addition is arithmetic.ถูกต้อง! ต้องแปลงข้อความเป็นตัวเลขก่อน จึงบวกแบบเลขคณิตได้

ข้อ 8

8 / 10

What is the text inside input("Enter name: ") for?ข้อความในวงเล็บของ input("Enter name: ") มีไว้ทำอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) A prompt shown before the program waits for the user to typeเป็นข้อความชวน (prompt) ที่แสดงก่อนรอให้ผู้ใช้พิมพ์

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) A prompt shown before the program waits for the user to typeเป็นข้อความชวน (prompt) ที่แสดงก่อนรอให้ผู้ใช้พิมพ์
    Correct! It is the prompt that tells the user what to type.ถูกต้อง! ข้อความนี้คือ prompt ที่บอกผู้ใช้ว่าให้พิมพ์อะไร
  • (B) A default value used if the user types nothingเป็นค่าเริ่มต้นที่โปรแกรมใช้ถ้าผู้ใช้ไม่พิมพ์
    Wrong: input() has no such default value.ผิด: input() ไม่มีค่าเริ่มต้นแบบนั้น
  • (C) The name of the variable that stores the resultเป็นชื่อของตัวแปรที่จะเก็บผลลัพธ์
    Wrong: the variable name is on the left of =, not in the brackets.ผิด: ชื่อตัวแปรอยู่ทางซ้ายของ = ไม่ใช่ในวงเล็บ
  • (D) An instruction that sets the type of data to readเป็นคำสั่งบอกชนิดข้อมูลที่ต้องการรับ
    Wrong: input() always returns a string regardless of the prompt text.ผิด: input() คืนค่าเป็นสตริงเสมอ ไม่ขึ้นกับข้อความ prompt

ข้อ 9

9 / 10

With age = input("Age: ") and the user typing 3, what does age * 2 give?age = input("Age: ") แล้วเขียน age * 2 จะได้อะไรถ้าผู้ใช้พิมพ์ 3?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) "33""33"

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 66
    Wrong: age is the string "3"; multiplying a string by 2 is not numeric.ผิด: age เป็นสตริง "3" การคูณสตริงด้วย 2 ไม่ได้คูณเลข
  • (B) An error, because you cannot multiply a stringError เพราะคูณสตริงไม่ได้
    Wrong: Python can multiply a string by an integer, which repeats the text.ผิด: Python คูณสตริงด้วยจำนวนเต็มได้ = ทำซ้ำข้อความ
  • (C) "33""33"
    Correct! "3" * 2 repeats the text to "33"; use int(age) first if you want 6.ถูกต้อง! "3" * 2 คือทำซ้ำข้อความได้ "33" ต้องใช้ int(age) ก่อนถ้าต้องการ 6
  • (D) "3 3""3 3"
    Wrong: repeating a string inserts no space; the result is "33".ผิด: การทำซ้ำสตริงไม่แทรกช่องว่าง ได้ "33"

ข้อ 10

10 / 10

To read a whole number from the user on one line, which line is correct?ต้องการรับจำนวนเต็มจากผู้ใช้ในบรรทัดเดียว บรรทัดใดถูกต้อง?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) n = int(input("n= "))n = int(input("n= "))

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) n = int input()n = int input()
    Wrong: calling a function needs brackets, e.g. int(...).ผิด: เรียกฟังก์ชันต้องมีวงเล็บ เช่น int(...)
  • (B) n = input(int())n = input(int())
    Wrong: this passes int() as the prompt and n is still a string.ผิด: แบบนี้ส่ง int() เป็น prompt และ n ยังเป็นสตริง
  • (C) n = int("n= ") + input()n = int("n= ") + input()
    Wrong: int("n= ") cannot convert non-numeric text and raises an error.ผิด: int("n= ") แปลงข้อความที่ไม่ใช่ตัวเลขไม่ได้ จะเกิด error
  • (D) n = int(input("n= "))n = int(input("n= "))
    Correct! input() reads the text first, then int() converts it to a whole number.ถูกต้อง! input() รับข้อความก่อน แล้ว int() แปลงเป็นจำนวนเต็ม