在请求输入时组合整数和字符串?

时间:2015-09-15 17:46:11

标签: string

这是一个非常基本的问题,因为我是计算机编程的新手,但我遇到了问题。问题是要求我在Python 3中编写一个矩形区域的等式,允许用户以平方英尺输入宽度和长度,并以平方英尺输出答案。这就是我尝试过的:

jj

但是,即使尝试输入附加了“平方”的整数,我也会遇到错误。有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

您必须将area分配部分更改为

area = str(length*width) + " feet squared."

那就是你必须在这里使用字符串连接运算符+

area = "{0} feet squared.".format(length*width)

答案 1 :(得分:0)

使用遵循相关规则的数字的示例答案:

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. 
相关问题