Skip to main content
 首页 » 编程设计

Python Traceback(最近调用最后)错误

2025年05月04日23pengyingh

我已经开始编码大约一个星期了,在练习创建一个形状计算器时,我遇到了这样的错误:

Traceback (most recent call last): 
File "python", line 4 
if option = 'C': 
          ^ 
SyntaxError: invalid syntax 

代码如下:

print "The Calculator has been launched" 
option = raw_input ("What shape is your object?     Enter C for circle or T  
for Triangle.") 
if option = 'C':  
    radius = float (raw_input ("What is the radius of your circle?") ) 
    area_1 = 3.14159 * ( radius ** 2) 
    print area_1  
 
elif option = 'T': 
    base = float (raw_input ("What is the base of the triangle?")) 
    height = float (raw_input ("What is the corresponding height of the  
    triangle?")) 
    area_2 = (base * height) * 1/2 
    print area  
else : 
    print "Please, enter a valid shape"  

如果有人能解释错误的原因,我将不胜感激。

谢谢!

请您参考如下方法:

比较时必须使用==。 = 仅用于赋值。

因此在您的示例中,该行应该是

if option == 'C':