-2

I got error of below code while placed in def : " UnboundLocalError: local variable 'czy_to_palindrome' referenced before assignment" It works fine without.

DOES NOT WORK:

while True:
    print("Podaj liczbe: ", end="")
    number = input()
    czy_to_palindrome = True
    def palindrome_check(number):
        for i in range(len(number)):
            if number[i] != number[len(number)- i - 1]:
                print("No, ", number, "is NOT a palindrome.")
                czy_to_palindrome = False
                break
        if czy_to_palindrome:
           print("Yes, ",number, "is a palindrome.")

    palindrome_check(number)

WORKS:

while True:
    print("Podaj liczbe: ", end="")
    number = input()
    czy_to_palindrome = True
    # def palindrome_check(number):
    for i in range(len(number)):
        if number[i] != number[len(number)- i - 1]:
            print("No, ", number, "is NOT a palindrome.")
            czy_to_palindrome = False
            break
    if czy_to_palindrome:
        print("Yes, ",number, "is a palindrome.")

    # palindrome_check(number)

Explain please what function change I do not understand. Thanks Begginer in codeing.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Look up "scope of variables" in python. – Pranav Hosangadi Oct 27 '22 at 21:04
  • Well, it looks like you want us to resolve your homework, kid. Look up global reserved work in python – Carmoreno Oct 27 '22 at 21:05
  • there is only one error on you inner function that `czy_to_palindrome` not defined in it try this it will access global `czy_to_palindrome` inside your `palindrome_check` function "`global czy_to_palindrome`" – Muhammad Akhlaq Mahar Oct 27 '22 at 21:05
  • Well nope, I am self-taught adult to be honest. Trying to get into codeing by myself and I found this exerises which I am working on right now for and hour and cannot find a solution.. – Micheal_new Oct 27 '22 at 21:07
  • You don't understand that you need to have `czy_to_palindrome = True` within the function. As a beginner you face the very common beginner problem with global/local scope of a name/identifier/variable where a line with assignment to a variable changes its scope within a function and lead to an error message. – Claudio Oct 27 '22 at 21:10
  • Just write inside the function: `global czy_to_palindrome`, just in the first line after the function definition. And, read about scope variables. Sorry if I was not polite, I think that you should read this: https://stackoverflow.com/help/how-to-ask Happy coding! – Carmoreno Oct 27 '22 at 21:11
  • Interesting thing is, that earlier I have added "global" inside but it didn't work but I could make some changes inside the code in the meanwhile. Now adding below works fine, thanks : """ def palindrome_check(number): global czy_to_palindrome """ – Micheal_new Oct 27 '22 at 21:12

1 Answers1

0

As they say in the comments, just add a global variable or pass the variable to the function. Here are the two options:

  while True:
        print("Podaj liczbe: ", end="")
        number = input()
        czy_to_palindrome = True
        def palindrome_check(number, czy_to_palindrome):
            for i in range(len(number)):
                if number[i] != number[len(number)- i - 1]:
                    print("No, ", number, "is NOT a palindrome.")
                    czy_to_palindrome = False
                    break
            if czy_to_palindrome:
               print("Yes, ",number, "is a palindrome.")
    
        palindrome_check(number, czy_to_palindrome)

Or

while True:
    print("Podaj liczbe: ", end="")
    number = input()
    czy_to_palindrome = True
    def palindrome_check(number):
        global czy_to_palindrome
        for i in range(len(number)):
            if number[i] != number[len(number)- i - 1]:
                print("No, ", number, "is NOT a palindrome.")
                czy_to_palindrome = False
                break
        if czy_to_palindrome:
           print("Yes, ",number, "is a palindrome.")

    palindrome_check(number)

The choice depends on what you want to achieve, if the variable is really a variable that you need to change in the rest of the program and stay that way, use 'global' otherwise use parameter passing.

FG94
  • 146
  • 4