无法使这个代码工作,简单的Python else if语句

时间:2017-08-07 17:50:40

标签: python if-statement

当我运行下面的代码时,我不断收到语法错误,并在显示的位置突出显示。

sf_population, sf_area = 864816, 231.89
rio_population, rio_area = 6453682, 486.5

sf_area = (int(sf_area))
rio_area = (int(rio_area))

def x= sf_population/sf_area
def y= rio_population/rio_area

if x<y:
    print"True"
if x>y:
    print"False"

3 个答案:

答案 0 :(得分:1)

工作示例:

sf_population, sf_area = 864816, 231.89
rio_population, rio_area = 6453682, 486.5

sf_area = int(sf_area)
rio_area = int(rio_area)

x = sf_population/sf_area
y = rio_population/rio_area

if x < y:
    print"True"

if x > y:
    print"False"

答案 1 :(得分:0)

在python中使用

def来定义函数,而不是变量。要将x的值设置为sf_population/sf_area,只需将def放在第5行和第6行。

答案 2 :(得分:0)

sf_population, sf_area = 864816, 231.89
rio_population, rio_area = 6453682, 486.5

sf_area = (int(sf_area))
rio_area = (int(rio_area))

x = sf_population/sf_area 
y = rio_population/rio_area

if x<y:
    print "True"
if x>y:
    print "False"

除非你想定义一个函数,否则不需要def 除此之外,请考虑将elif用于第二个条件

if x<y:
    print "True"
elif x>y:
    print "False"