在python中运行以下程序时出错

时间:2018-02-05 11:07:47

标签: python syntax

在python中运行以下程序时出错

data = read_data()
batsman_name = first_batsman(data)
def first_batsman(data=data):
    innings = data['innings'][0]
first_innings = innings['1st innings']
deliveries = first_innings['deliveries']
first_ball =(deliveries[0] [0.1])
first_batsman = first_ball['batsman']
    return(first_batsman)
print(first_batsman)

收到错误:

  

返回(first_batsman)
  ^
  IndentationError:意外缩进

我甚至尝试过以下程序:

data = read_data()
batsman_name = first_batsman(data)
def first_batsman(data=data):
    innings = data['innings'][0]
first_innings = innings['1st innings']
deliveries = first_innings['deliveries']
first_ball =(deliveries[0] [0.1])
first_batsman = first_ball['batsman']
return(first_batsman)
print(first_batsman)

但仍然得到错误:

  

返回(first_batsman)
  SyntaxError:' return'外部功能

这是什么解决方案?

1 个答案:

答案 0 :(得分:0)

我认为你误解了意图:may read this

对于您的代码,您可能想尝试这样的事情:

class First_Batsman( object ) :
    def __init__( self ):
        self.innings = data['innings'][0]
        self.first_innings = innings['1st innings']
        self.deliveries = first_innings['deliveries']
        self.first_ball = deliveries[0][0.1]
        self.first_batsman = first_ball['batsman']

    def __str__(self):
        return str(self.innings,self.first_innings,self.deliveries,
                   self.first_ball,self.first_batsman)

data = read_data()
batsman_name = first_batsman( data )
print( first_batsman )