Lesson 05: Input/Output

🎯 Mục Tiêu

  • Nhận input từ người dùng
  • Hiểu type conversion với input
  • Output formatting nâng cao
  • File I/O cơ bản

Input từ User

# Nhận input (luôn là string)
name = input("Tên bạn là gì? ")
print(f"Xin chào, {name}!")

# Convert sang số
age = input("Tuổi của bạn: ")
age = int(age)  # String → Int
print(f"Năm sau bạn {age + 1} tuổi")

# Gộp lại
age = int(input("Tuổi: "))

Type Conversion với Input

# Integer
number = int(input("Nhập số: "))

# Float
price = float(input("Giá: "))

# Xử lý lỗi
try:
    age = int(input("Tuổi: "))
except ValueError:
    print("Vui lòng nhập số!")
# Separator
print("A", "B", "C", sep="-")  # A-B-C

# End
print("Line 1", end=" ")
print("Line 2")  # Line 1 Line 2

# Multiple values
name = "An"
age = 25
print(name, age, sep=" | ")  # An | 25

➡️ Bài Tiếp Theo Lesson 06 – If/Else

Leave a Reply

Your email address will not be published. Required fields are marked *