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

Python TypeError: can't multiply sequence by non-int of type 'str' - แก้ไขอย่างไร?

ใน Python คูณตัวเลข 2 ตัวได้ คูณตัวเลขกับ string ก็ได้ คืนค่า sequence ของ string ที่ซ้ำตามจำนวนที่กำหนด

แต่ถ้าพยายามคูณ string กับ string จะเจอ error TypeError: can't multiply sequence by non-int of type 'str'

คู่มือนี้อธิบาย error นี้หมายถึงอะไร และเจอที่ไหนในโค้ด พร้อมตัวอย่าง ช่วยให้คุณแก้ได้

TypeError: can't multiply sequence by non-int of type 'str'

Strings เป็นประเภทของ sequence เพราะมี characters ที่ Python iterate ได้ Sequence types อื่น: tuples, dictionaries, lists

ใช้ multiplication operator (*) สร้าง string ที่ซ้ำเนื้อหา ตัวอย่าง:

print("Cakes! " * 2)

คืนค่า: "Cakes! Cakes!" multiplication operator ทำให้ string ซ้ำ 2 ครั้ง

ไม่สามารถใช้ multiplication operator คูณ string กับ string ได้ เฉพาะ integers และ floating-point numbers ที่คูณกับค่า data type เดียวกันได้ Python ไม่มีวิธี interpret การคูณ 2 strings

ตัวอย่างสถานการณ์ (An Example Scenario)

สร้างโปรแกรมคำนวณเงินที่ร้านอาหารได้จากขนมสโคนแยม ช่วงบ่ายวันพฤหัสบดี

เริ่มกำหนดราคาสโคนแยม ขอผู้ใช้บอกว่าขายสโคนแยมกี่ชิ้นเมื่อวาน ด้วย input():

jam_scone = "1.95"
sold = input("How many jam scones did you sell yesterday? ")

ต่อไป คูณค่าทั้ง 2 เข้าด้วยกัน จะได้เงินที่ได้จากสโคนในวันนั้น:

earned_from_scones = jam_scone * sold

ตอนรู้รายได้จากสโคนแล้ว เขียน print() แจ้งผู้ใช้ผลลัพธ์:

print("You earned ${} from selling scones.".format(earned_from_scones))

format() method ใส่ค่า "earned_from_scones" ที่ curly braces ({}) ใน string

ต้องคำนวณกำไรต่อสโคนด้วย รู้ว่าทำกำไร 0.25 ต่อสโคน คำนวณกำไรรวม:

profit = sold * 0.25
print("You made a ${} profit from selling scones.".format(profit))

โค้ดคำนวณกำไรต่อสโคน แล้วพิมพ์ค่า รันโค้ดดู:

How many jam scones did you sell yesterday? 17
Traceback (most recent call last):
File "main.py", line 4, in 
earned_from_scones = jam_scone * sold
TypeError: can't multiply sequence by non-int of type 'str'

โค้ด error

วิธีแก้ (The Solution)

วิเคราะห์บรรทัดที่ error ชี้:

earned_from_scones = jam_scone * sold

บรรทัดนี้ดูปกติ แต่มีปัญหา: พยายามคูณ 2 string values เข้าด้วยกัน "jam_scone" เก็บเป็น string input() คืนค่า string หมายถึง "sold" เป็น string

แก้: ต้องทำให้ "jam_scone" และ "sold" เป็น floating points ทั้งคู่ เพื่อทำ mathematical operation ได้

เปลี่ยนการประกาศค่าตัวแปร:

jam_scone = 1.95
sold = float(input("How many jam scones did you sell yesterday? "))

"jam_scone" ไม่มี quotation marks แสดงว่าเปลี่ยนจาก string เป็น float ใช้ float() แปลง "sold" เป็น floating-point number

รันโค้ด:

How many jam scones did you sell yesterday? 17
You earned $33.15 from selling scones.
You made a $4.25 profit from selling scones.

โค้ดทำงานสำเร็จ ขอผู้ใช้ใส่จำนวนสโคน คำนวณรายได้จากสโคน แล้วคำนวณกำไรจากการขายสโคน

สรุป

Error TypeError: can't multiply sequence by non-int of type 'str' เกิดถ้าพยายามคูณ 2 string values เข้าด้วยกัน แก้: ให้แน่ใจว่าคูณ numerical values กัน หรือคูณ string กับ integer เท่านั้น

ตอนนี้คุณพร้อมแก้ error นี้อย่าง Pythonista แล้ว!