python 2.7 def错误

时间:2015-02-22 11:54:04

标签: python function

def h(x):
     x = ((x[0])*len(x))
     return x

当我打印它时,它会

h(he)

Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    h(he)
NameError: name 'he' is not defined

1 个答案:

答案 0 :(得分:1)

您是否可能意味着将he作为字符串传递? 'he'将是适当的方式。

def h(x):
     x = ((x[0])*len(x))
     return x

print(h('he'))

>>> hh