Devdit
 

TypeError: can only concatenate str (not int) to str คือ วิธีแก้ไข

1.1K

รันโปรแกรมภาษา Python แล้วขึ้นข้อความว่า TypeError: can only concatenate str (not "int") to str ปัญหานี้เกิดจากอะไร ต้องแก้ไขอย่างไร

a = 10
b = 20
c = a + b
print( 'a + b = '+c )

 

วิธีแก้ไข

ปัญหา TypeError: can only concatenate str (not "int") to str เกิดจากไม่สามารถนำตัวแปร c ที่เป็นชนิด int ไปเชื่อมกับประโยคข้อความได้ วิธีแก้ไขให้ทำการแปลงตัวแปร c เป็นให้ชนิด string ผ่านคำสั่ง str

a = 10
b = 20
c = a + b
print( 'a + b = '+str(c) )

ผลลัพธ์

30
แก้ไข 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ