You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python新手求助:计算矩形面积时程序报错

Fixing Your Rectangle Area/Perimeter Python Code

Hey there! Let's walk through fixing your rectangle area calculation code step by step—no worries, syntax errors are super common when you're just starting out with Python. 😊

First, let's break down the issues in your original code:

  • Both of your input() calls are missing a closing parenthesis ) — Python can't parse incomplete statements like this, so it'll throw a syntax error right away.
  • When calculating the perimeter, you used the variable name breadth, but you defined the width as width—variable names need to match exactly in Python.
  • Your final print() statement is missing a comma between the string and the Area variable. Python needs this separator to know how to combine the text and the value for output.

Here's the corrected version of your code, with comments pointing out the fixes:

# Python Program to find Area and Perimeter of a Rectangle
# Fixed: Added closing parenthesis to input() calls
width = float(input('Enter width of rectangle : '))
length = float(input('Enter length of rectangle : '))

# Calculating Area
Area = width * length
# Fixed: Changed 'breadth' to 'width' to match the variable we defined
Perimeter = 2 * (length + width)

# Fixed: Added comma between string and variable, cleaned up whitespace
print('Area of Rectangle :', Area)
# Optional: Add a print statement for perimeter too!
print('Perimeter of Rectangle :', Perimeter)

If you run this code now, it should work perfectly—just enter the width and length when prompted, and it'll output both the area and perimeter correctly.

内容的提问来源于stack exchange,提问作者Aditya Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:50:03