Python 2.7 和随机
Python 2.7 and random
我正在尝试 运行 以下代码...
# -*- coding: utf-8 -*-
import random
def randomList():
l = []
for i in range(0, 20):
l.append(random.randint(-100, 100)
return l
def displayList (l):
for i in l:
print (i)
listeAleatoire = randomList()
displayList( listeAleatoire )
但是,在 return 处,它显示以下错误:
"[...], line 8 return l SyntaxError: invalid syntax"
^
我有点无助,也许你能帮帮我,在我把头发弄掉之前...谢谢!
你错过了这一行的右括号:
l.append(random.randint(-100, 100)
^-- should be one more here
所以 Python 抱怨 return 语句,因为它认为它仍然在上一行的括号内。
我为您重新格式化了代码。您应该关闭所有开括号。请参阅脚本中的注释。
# -*- coding: utf-8 -*-
import random
def randomList():
l = []
for i in range(0, 20):
l.append(random.randint(-100, 100)) # close the bracket!
return l
def displayList(l):
for i in l:
print (i)
listeAleatoire = randomList()
displayList(listeAleatoire)
我正在尝试 运行 以下代码...
# -*- coding: utf-8 -*-
import random
def randomList():
l = []
for i in range(0, 20):
l.append(random.randint(-100, 100)
return l
def displayList (l):
for i in l:
print (i)
listeAleatoire = randomList()
displayList( listeAleatoire )
但是,在 return 处,它显示以下错误:
"[...], line 8 return l SyntaxError: invalid syntax"
^
我有点无助,也许你能帮帮我,在我把头发弄掉之前...谢谢!
你错过了这一行的右括号:
l.append(random.randint(-100, 100)
^-- should be one more here
所以 Python 抱怨 return 语句,因为它认为它仍然在上一行的括号内。
我为您重新格式化了代码。您应该关闭所有开括号。请参阅脚本中的注释。
# -*- coding: utf-8 -*-
import random
def randomList():
l = []
for i in range(0, 20):
l.append(random.randint(-100, 100)) # close the bracket!
return l
def displayList(l):
for i in l:
print (i)
listeAleatoire = randomList()
displayList(listeAleatoire)