-1

Getting error while running a below program in 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)

getting error as :

return(first_batsman)
^
IndentationError: unexpected indent

I even tried below program :

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)

But still getting error as :

return(first_batsman)
SyntaxError: 'return' outside function

What is the solution for this?

vindev
  • 2,240
  • 2
  • 13
  • 20
  • The solution is to use `return` only inside a function (and to make sure that it is indented properly, with respect to the scope it is located in). – goodvibration Feb 05 '18 at 11:09
  • Everything that you want inside your `first_batsman()` function must be indented, and that includes the `return()`. So `first_innings = ...` signals to Python the end of the function is above it, as it is not indented. – Martin Evans Feb 05 '18 at 11:11
  • @Harsha what are you talking about? you can return a value from anywhere as long as you are in a function. – mdeous Feb 05 '18 at 13:39

1 Answers1

0

I think you missunderstood intentations: may read this

To your code, you may want to try something like 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 )
Skandix
  • 1,916
  • 6
  • 27
  • 36