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

โปรแกรมอ่านค่าจากคอนโซลใน Python

สมมติว่าเราต้องรับ firstname และ lastname จากคอนโซล แล้วแสดงข้อความ "Hello , you are welcome!" ใช้ format() กับ placeholder {} ในสตริง

ตัวอย่าง Input: Ashish Dutta ผลลัพธ์: "Hello Ashish Dutta, you are welcome!"

ขั้นตอนการแก้ปัญหา

  • fn := input() รับบรรทัดแรก
  • ln := input() รับบรรทัดที่สอง
  • ret := "Hello {} {}, you are welcome!".format(fn, ln)
  • คืนค่า ret

ตัวอย่างโค้ด

def solve():
    fn = input()
    ln = input()
    ret = "Hello {} {}, you are welcome!".format(fn, ln)
    return ret

print(solve())

อินพุต

Ashish Dutta

เอาต์พุต

Hello Ashish Dutta, you are welcome!