Python: Passing Function Arguments for strings

nam

I am practicing with "Think Python", Exercise 8.1 that:

"Write a function that takes a string as an argument and displays the letters backward, one per line."

I am able to do this question, by using banana as an example to print each letter per line.

index = 0
fruit = "banana"
while index < len(fruit):
    letter = fruit[len(fruit)-index-1]
    print letter
    index = index + 1

However, I would like to generalize the situation to any input words and I got the problem, my code is

index = 0
def apple(fruit):
    while index < len(fruit):
        letter = fruit[len(fruit)-index-1]
        print letter
        index = index + 1

apple('banana')

The corresponding errors are:

Traceback (most recent call last):
  File "exercise8.1_mod.py", line 21, in <module>
    apple('banana')
  File "exercise8.1_mod.py", line 16, in apple
    while index < len(fruit):
UnboundLocalError: local variable 'index' referenced before assignment

I think there should be problems concerned with the function arguments used. Any helps will be appreciated.

sundar nataraj

your program got error due to your accessing a global variable in your method and trying to change its value

index = 0
def apple(fruit):
    .....
    index = index + 1
    ....    
apple('banana')

this give you error UnboundLocalError: local variable 'index' referenced before assignment

but if you give

def apple(fruit):
        global index
        .....
        index = index + 1
        .... 

this produces correct result

in python we have Global variable and Local variables

please go throught this

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it as global.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: passing arguments to a function

From Dev

Python: Passing function with arguments to a function

From Dev

passing arguments to a callback function in Python

From Python

Passing arguments into function in tkinter python

From Dev

Passing arguments to a nested function in Python

From Dev

passing strings as arguments in ddply

From Dev

Python; Why passing function arguments not mandatory

From Dev

Python: passing some arguments to a function-argunment

From Dev

Passing a function with two arguments to filter() in python

From Dev

Passing more arguments to this type of python function

From Java

Passing functions with arguments to another function in Python?

From Dev

create a main function in python and passing arguments

From Dev

Passing tuples with default arguments to a function in Python?

From Dev

Python passing arguments through a chain of function calls

From Dev

Calling a python function from R with passing the arguments

From Dev

Passing arguments to Python function as a data structure

From Dev

Passing list as normal arguments to function in python

From Dev

Passing a variable number of arguments to a function in Python

From Dev

Appending variables as strings when passing command line arguments in python 2.7.12

From Dev

Passing arguments to function with named arguments

From Dev

Passing range as function(arguments)

From Dev

Passing a function as a template arguments

From Dev

Passing arguments to a callback function

From Dev

Passing Arguments to Inner Function

From Dev

Passing variable arguments to function

From

Passing arrays as function arguments

From Dev

Passing a function and arguments to a thread

From Dev

Passing arguments to decorator function

From Dev

Passing multiple arguments to a function?

Related Related

  1. 1

    Python: passing arguments to a function

  2. 2

    Python: Passing function with arguments to a function

  3. 3

    passing arguments to a callback function in Python

  4. 4

    Passing arguments into function in tkinter python

  5. 5

    Passing arguments to a nested function in Python

  6. 6

    passing strings as arguments in ddply

  7. 7

    Python; Why passing function arguments not mandatory

  8. 8

    Python: passing some arguments to a function-argunment

  9. 9

    Passing a function with two arguments to filter() in python

  10. 10

    Passing more arguments to this type of python function

  11. 11

    Passing functions with arguments to another function in Python?

  12. 12

    create a main function in python and passing arguments

  13. 13

    Passing tuples with default arguments to a function in Python?

  14. 14

    Python passing arguments through a chain of function calls

  15. 15

    Calling a python function from R with passing the arguments

  16. 16

    Passing arguments to Python function as a data structure

  17. 17

    Passing list as normal arguments to function in python

  18. 18

    Passing a variable number of arguments to a function in Python

  19. 19

    Appending variables as strings when passing command line arguments in python 2.7.12

  20. 20

    Passing arguments to function with named arguments

  21. 21

    Passing range as function(arguments)

  22. 22

    Passing a function as a template arguments

  23. 23

    Passing arguments to a callback function

  24. 24

    Passing Arguments to Inner Function

  25. 25

    Passing variable arguments to function

  26. 26

    Passing arrays as function arguments

  27. 27

    Passing a function and arguments to a thread

  28. 28

    Passing arguments to decorator function

  29. 29

    Passing multiple arguments to a function?

HotTag

Archive