Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ฟังก์ชัน input() ใน Python ทำอะไร?

input() แสดง prompt รับ input จากผู้ใช้

  • Python 2.x: คืนค่าที่ Python interpret (พิมพ์ "Hello" ได้ string, พิมพ์ 5 ได้ int)
  • Python 3.x: คืน string เสมอ

ตัวอย่าง

name = raw_input("What is your name? ")
print "Hello, %s." % name

หมายเหตุ: ตัวอย่างใช้ raw_input() ซึ่งเป็น Python 2