Hướng dẫn how do you concatenate a string and a float in python? - làm thế nào để bạn nối một chuỗi và một dấu phẩy trong python?

Giáo viên hình học của chúng tôi đã cho chúng tôi một nhiệm vụ yêu cầu chúng tôi tạo ra một ví dụ về khi nào đồ chơi sử dụng hình học trong cuộc sống thực, vì vậy tôi nghĩ sẽ rất tuyệt khi tạo ra một chương trình tính toán có bao nhiêu gallon nước để lấp đầy một nhóm hình dạng, và với các kích thước nhất định.

Show

Đây là chương trình cho đến nay:

import easygui
easygui.msgbox("This program will help determine how many gallons will be needed to fill up a pool based off of the dimensions given.")
pool=easygui.buttonbox("What is the shape of the pool?",
              choices=['square/rectangle','circle'])
if pool=='circle':
height=easygui.enterbox("How deep is the pool?")
radius=easygui.enterbox("What is the distance between the edge of the pool and the center of the pool (radius)?")
easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")

Tôi vẫn nhận được lỗi này mặc dù:

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects

tôi làm gì?

Hướng dẫn how do you concatenate a string and a float in python? - làm thế nào để bạn nối một chuỗi và một dấu phẩy trong python?

Nircraft

7.7565 huy hiệu vàng29 Huy hiệu bạc45 Huy hiệu đồng5 gold badges29 silver badges45 bronze badges

Đã hỏi ngày 5 tháng 6 năm 2013 lúc 19:31Jun 5, 2013 at 19:31

0

Tất cả các loại dữ liệu phao hoặc không chuỗi phải được đúc vào chuỗi trước khi kết nối

Điều này sẽ hoạt động chính xác: (Lưu ý diễn viên

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
2 cho kết quả nhân)

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")

Trực tiếp từ thông dịch viên:

>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.

Đã trả lời ngày 28 tháng 6 năm 2013 lúc 13:23Jun 28, 2013 at 13:23

Kalyan02Kalyan02Kalyan02

1.39610 Huy hiệu bạc16 Huy hiệu đồng10 silver badges16 bronze badges

2

Có một giải pháp nữa, bạn có thể sử dụng định dạng chuỗi (tương tự như ngôn ngữ C tôi đoán)

Bằng cách này, bạn cũng có thể kiểm soát độ chính xác.

radius = 24
height = 15

msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

không có độ chính xác

Bạn cần 27129.600000 gallon nước để lấp đầy hồ bơi này.

Với độ chính xác 8.2

Bạn cần 27129,60 gallon nước để lấp đầy hồ bơi này.

Đã trả lời ngày 17 tháng 5 năm 2019 lúc 19:00May 17, 2019 at 19:00

Gaurang Shahgaurang ShahGaurang Shah

10,9k5 huy hiệu vàng68 Huy hiệu bạc124 Huy hiệu đồng5 gold badges68 silver badges124 bronze badges

Với Python3.6+, bạn có thể sử dụng F-Strings để định dạng các câu lệnh in.

radius=24.0
height=15.0
print(f"You need {3.14*height*radius**2:8.2f} gallons of water to fill this pool.")

Đã trả lời ngày 3 tháng 10 năm 2019 lúc 0:50Oct 3, 2019 at 0:50

TyberiustyberiusTyberius

6252 Huy hiệu vàng12 Huy hiệu bạc20 Huy hiệu đồng2 gold badges12 silver badges20 bronze badges

Bài viết này mô tả cách kết hợp các chuỗi trong Python.

  • Concatenate nhiều chuỗi:
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    3, nhà điều hành
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    4
  • Các chuỗi và số Concatenate:
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    3, toán tử
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    4,
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    7,
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    8, F-String
  • Concatenate một danh sách các chuỗi thành một chuỗi:
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    9
  • Concatenat một danh sách các số vào một chuỗi:
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    9,
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    7

Concatenate nhiều chuỗi: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 3, nhà điều hành easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 4

Các chuỗi và số Concatenate: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 3, toán tử easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 4, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 7, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 8, F-String

Concatenate một danh sách các chuỗi thành một chuỗi:

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9

s = 'aaa' + 'bbb' + 'ccc'
print(s)
# aaabbbccc

s1 = 'aaa'
s2 = 'bbb'
s3 = 'ccc'

s = s1 + s2 + s3
print(s)
# aaabbbccc

s = s1 + s2 + s3 + 'ddd'
print(s)
# aaabbbcccddd

Concatenat một danh sách các số vào một chuỗi: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 9, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 7

Nhà điều hành

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3

s1 += s2
print(s1)
# aaabbb

Bạn có thể kết hợp các chuỗi chữ (

>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.
5 hoặc
>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.
6) và các biến chuỗi với toán tử
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3.

s = 'aaa'

s += 'xxx'
print(s)
# aaaxxx

Nhà điều hành easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 4

Bạn có thể nối một chuỗi khác vào một chuỗi với toán tử tại chỗ,

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4. Chuỗi bên phải được nối sau khi biến chuỗi ở bên trái.

s = 'aaa''bbb''ccc'
print(s)
# aaabbbccc

Nếu bạn muốn thêm một chuỗi vào cuối biến chuỗi, hãy sử dụng toán tử

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
0

Concatenate bằng cách viết chuỗi chữ liên tiếp

  • Nếu bạn viết chuỗi chữ liên tiếp, chúng được nối.

Ngay cả khi có nhiều không gian hoặc dòng mới với dấu gạch chéo ngược

radius = 24
height = 15

msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)

msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)
1 (được coi là các dòng tiếp tục) giữa các chuỗi, chúng vẫn được nối.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
1

Các chuỗi và số Concatenate: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 3, toán tử easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 4, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 7, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 8, F-String

Concatenate một danh sách các chuỗi thành một chuỗi:

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
2

Concatenat một danh sách các số vào một chuỗi:

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9,
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
7

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
3

Nhà điều hành

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3

  • Bạn có thể kết hợp các chuỗi chữ (
    >>> radius = 10
    >>> height = 10
    >>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    >>> print msg
    You need 3140.0gallons of water to fill this pool.
    
    5 hoặc
    >>> radius = 10
    >>> height = 10
    >>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    >>> print msg
    You need 3140.0gallons of water to fill this pool.
    
    6) và các biến chuỗi với toán tử
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    3.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
4

Nhà điều hành

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
5

Bạn có thể nối một chuỗi khác vào một chuỗi với toán tử tại chỗ,

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4. Chuỗi bên phải được nối sau khi biến chuỗi ở bên trái.

  • Nếu bạn muốn thêm một chuỗi vào cuối biến chuỗi, hãy sử dụng toán tử
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    4.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
6

Concatenate một danh sách các chuỗi thành một chuỗi: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 9

Concatenat một danh sách các số vào một chuỗi:

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
9,
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
7

  • Nhà điều hành
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    3

Bạn có thể kết hợp các chuỗi chữ (

>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.
5 hoặc
>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.
6) và các biến chuỗi với toán tử
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3.

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
7

Nhà điều hành

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
8

Bạn có thể nối một chuỗi khác vào một chuỗi với toán tử tại chỗ,

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4. Chuỗi bên phải được nối sau khi biến chuỗi ở bên trái.

Nếu bạn muốn thêm một chuỗi vào cuối biến chuỗi, hãy sử dụng toán tử

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4.

  • Concatenate bằng cách viết chuỗi chữ liên tiếp

Concatenat một danh sách các số vào một chuỗi: easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 9, easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 7

Nhà điều hành

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3

easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height))

+ "gallons of water to fill this pool.")
TypeError: cannot concatenate 'str' and 'float' objects
9

Bạn có thể kết hợp các chuỗi chữ (

>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.
5 hoặc
>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.
6) và các biến chuỗi với toán tử
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
3.

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
0

Nhà điều hành

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
1

Bạn có thể nối một chuỗi khác vào một chuỗi với toán tử tại chỗ,

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
4. Chuỗi bên phải được nối sau khi biến chuỗi ở bên trái.

  • Nếu bạn muốn thêm một chuỗi vào cuối biến chuỗi, hãy sử dụng toán tử
    easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
    
    4.

Xem bài viết sau đây để biết chi tiết về sự hiểu biết danh sách và biểu thức máy phát.

  • Liệt kê sự hiểu biết trong Python