Palindrome doesn't work inside of a function
Question:
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.
Answers:
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.
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.
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.