2017-08-07 63 views
-3

當我運行下面的代碼時,我總是收到一個語法錯誤,它突出顯示在哪裏顯示。

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" 
+2

這不是'def'作品。 – user2357112

+0

確定的事情:'def x = stuff'是一個語法錯誤。 – ForceBru

+0

無論如何,你甚至爲什麼還要把'def'放在那裏? – user2357112

回答

0

def用於python來定義函數,而不是變量。要設置的xsf_population/sf_area值簡單的丟棄在線5的def和6

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" 
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" 
+0

謝謝你的幫助! –