String 请求输入时合并整数和字符串?

String 请求输入时合并整数和字符串?,string,String,这是一个非常基本的问题,因为我是计算机编程新手,但我遇到了一些问题。问题是让我用Python3编写一个矩形面积的公式,允许用户以平方英尺为单位输入宽度和长度,并以平方英尺为单位输出答案。这就是我尝试过的: width = int(input("What is the width of the rectangle?") 10 ft^2 length = int(input("What is the length of the rectangle?") 5 ft^2

这是一个非常基本的问题,因为我是计算机编程新手,但我遇到了一些问题。问题是让我用Python3编写一个矩形面积的公式,允许用户以平方英尺为单位输入宽度和长度,并以平方英尺为单位输出答案。这就是我尝试过的:

width = int(input("What is the width of the rectangle?")
        10 ft^2
length = int(input("What is the length of the rectangle?")
        5 ft^2
area = str(length*width("feet squared.")

但我甚至在输入一个带有“英尺平方”的整数时都会出错。有人能帮我吗?

您必须将
区域
分配部分更改为

area = str(length*width) + " feet squared."
也就是说,您必须在此处使用字符串连接运算符
+


使用数字的示例回答问题中所述的规则:

width = input("What is the width of the rectangle?"
# What is the width of the rectangle? 10
length = input("What is the length of the rectangle?")
# What is the width of the rectangle? 5
area = "{0} feet squared.".format(length*width)
print(area)
# 50 feet squared. 

长度和宽度应以英尺为单位,只需输入一个数字而不是
ft^2
,您可以将
int(…)
更改为
parseInt(…)
…以平方英尺为单位输入两个长度值?区域分配的第二个修改“area=“{0}英尺平方”。.format(length*width)”有效,但第一个没有,有任何原因吗?也非常感谢你的帮助。第一个适合我。单击“接受”按钮为回答者评分。
width = input("What is the width of the rectangle?"
# What is the width of the rectangle? 10
length = input("What is the length of the rectangle?")
# What is the width of the rectangle? 5
area = "{0} feet squared.".format(length*width)
print(area)
# 50 feet squared.