首页 > 解决方案 > Python Traceback(最近一次调用最后一次)错误

问题描述

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

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" 

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

谢谢!

标签: pythontraceback

解决方案


比较时必须使用==. =仅用于分配。

所以在你的例子中,这条线应该是

if option == 'C':

推荐阅读