获取TypeError:无法将序列与“str”类型的nonint相乘

2024-04-26 11:25:46 发布

您现在位置:Python中文网/ 问答频道 /正文

TypeError Full code #矩形输入

print("~" * 60)
print("~" * 60)
lengthOne = str(int(input("Enter the Length of Rectangle #01 = ")));
widthOne = str(int(input("Enter the Width of Rectangle #01 = ")));
areaRecOne = (lengthOne * widthOne)
print("~" * 60)
lengthTwo = str(input("Enter the Length of Rectangle #02 = "));
widthTwo = str(input("Enter the Width of Rectangle #02 = "));

#Output of Rectangles
print("~" * 60)
print("~" * 60)
print("Length of Rectangle #01 = " + lengthOne + " SQ Inches");
print("Width of Rectangle #01 = " + widthOne + " SQ Inches");
print("Area of Rectangle #01 = " + areaRecOne + " SQ Inches");
print("~" * 60)
print("Length of Rectangle #02 = " + lengthTwo + " SQ Inches");
print("Width of Rectangle #02 = " + widthTwo + " SQ Inches");
print("~" * 60)
print("~" * 60)
#Calculation which Rectangle is greater

我刚开始上COP1000课,我一直在解决这个不断出现的问题。我尝试了我所学到的一切,但没有结果。你知道吗

我需要的是让代码计算矩形的面积并统计哪个矩形更大

但每次乘法都是这么说的

    areaRecOne = (lengthOne * widthOne)
    TypeError: can't multiply sequence by non-int of type 'str'

提前谢谢!你知道吗


Tags: oftheinputsqwidthlengthintprint
1条回答
网友
1楼 · 发布于 2024-04-26 11:25:46

看起来您正在将输入转换为int(这很好),然后再转换回str(这就是问题所在)。你知道吗

试试看

lengthOne = int(input('...'))
widthOne = int(input('...'))

另外,我建议在Python中使用snake_case;它更规范。你知道吗

编辑:

这里有一个简化版本的程序,供您根据您的实现。你知道吗

print("~" * 60)
length = int(input("Length = "));
width = int(input("Width = "));

print("~" * 60)
area = length * width
print("Area = " + str(area))

相关问题 更多 >