Posts: 201
Threads: 37
Joined: Dec 2021
Hi,
How is it possible to use an if statement from another function. I am suppose to determine from the other function def inverserChaine(self): if it is a palindrome or not. I cant access the if condition from def inverserChaine(self):.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Palindrome:
def __init__( self ):
self .chaine = ''
def inverserChaine( self ):
for carac in self .chaine:
inversion = carac + inversion
return inversion
def estUnPalindrome( self ):
if inversion = = self .chaine:
O = "C'est un palindrome"
return O
else :
N = 'Ce n\'est pas un palindrome'
return N
monPal = Palindrome()
|
Thank you
Posts: 379
Threads: 2
Joined: Jan 2021
This would be one way to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Palindrome:
def inverserChaine( self , chaine):
inversion = ''
for carac in chaine:
inversion = carac + inversion
return inversion
def estUnPalindrome( self , chaine):
if self .inverserChaine (chaine) = = chaine:
O = "C'est un palindrome"
return O
else :
N = 'Ce n\'est pas un palindrome'
return N
monPal = Palindrome()
print (monPal.estUnPalindrome( 'abba' ))
|
Posts: 201
Threads: 37
Joined: Dec 2021
A big Thank you to you!
Now i have a better understanding of how it works.
Posts: 7,316
Threads: 123
Joined: Sep 2016
Feb-28-2022, 07:58 PM
(This post was last modified: Feb-28-2022, 07:58 PM by snippsat.)
To make some correction in language and style,like it' s not functions but methods that belong to the class Palindrome.
CamelCase🐫 look at PEP-8 and a song🎵 that make it clear.
The it will be like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Palindrome:
def inverser_chaine( self , chaine):
inversion = ''
for carac in chaine:
inversion = carac + inversion
return inversion
def est_palindrome( self , chaine):
if self .inverser_chaine(chaine) = = chaine:
return "C'est un palindrome"
return "Ce n'est pas un palindrome"
@staticmethod
def bar():
return 100
mon_pal = Palindrome()
print (mon_pal.est_palindrome( 'abba' ))
|
Posts: 201
Threads: 37
Joined: Dec 2021
 crazy way to teach coding.
Good info. Thank you
Ultimatly i came up with this:
As long as it respect teacher's demands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import tkinter as tk
class Palindrome:
def inverserChaine( self , chaine):
inversion = ''
for carac in chaine:
inversion = carac + inversion
return inversion
def estUnPalindrome( self , chaine):
if self .inverserChaine(chaine) = = chaine:
O = "C'est un palindrome"
return O
else :
N = 'Ce n\'est pas un palindrome'
return N
def tester():
monPal = Palindrome()
monPal.chaine = str (entD1.get())
txtPal.configure(text = 'Es-ce une palindrome? ' + str (monPal.estUnPalindrome(monPal.chaine)))
fen = tk.Tk()
fen.title( 'Palindrome' )
txtD1 = tk.Label(fen, text = 'Entrez votre mot ici:' )
txtD1.grid(row = 0 , column = 0 , sticky = tk.E)
entD1 = tk.Entry(fen)
entD1.grid(row = 0 , column = 1 )
bouton = tk.Button(fen, text = "Tester" , command = tester)
bouton.grid(row = 3 , column = 2 , columnspan = 2 )
txtPal = tk.Label(fen, text = 'Es-ce une palindrome?' )
txtPal.grid(row = 3 , column = 0 , columnspan = 2 , sticky = tk.W)
fen.mainloop()
|
Posts: 6,788
Threads: 20
Joined: Feb 2020
Stop doing this:
1 2 3 4 5 6 |
if inversion = = self .chaine:
O = "C'est un palindrome"
return O
else :
N = 'Ce n\'est pas un palindrome'
return N
|
First off you should not use one letter variables. Second, variables should be snake_case with no upper case letters. Third, what is the purpose of "O" or "N"? Why assign these variables at all? Finally, if you return out of an if/else, why do you have the else?
1 2 3 |
if inversion = = self .chaine:
return "C'est un palindrome"
return 'Ce n\'est pas un palindrome'
|
And why are you doing this?
1 |
txtPal.configure(text = 'Es-ce une palindrome? ' + str (monPal.estUnPalindrome(monPal.chaine)))
|
What does Palindrome.estUnPalindrome() return? Does it return a str? If it returns a str, why are you doing str(str)?
You need to think about what you are typing. Be deliberate. Be concise.
Frankduc and ndc85430 like this post
Posts: 201
Threads: 37
Joined: Dec 2021
dean,
I change it for :
1 2 3 4 |
if self .inverserChaine(chaine) = = chaine:
return "C'est un palindrome"
else :
return 'Ce n\'est pas un palindrome'
|
You need to reference the first method otherwise its not working. Not on my side.
This part:
1 |
txtPal.configure(text = 'Es-ce une palindrome? ' + str (monPal.estUnPalindrome(monPal.chaine)))
|
Return the phrase 'Is this a palindrome?' and next to it return 'Yes it is' or 'No, Its not'
Posts: 6,788
Threads: 20
Joined: Feb 2020
This code:
1 2 3 4 |
if self .inverserChaine(chaine) = = chaine:
return "C'est un palindrome"
else :
return 'Ce n\'est pas un palindrome'
|
does not need (should not have) the else:
1 2 3 |
if self .inverserChaine(chaine) = = chaine:
return "C'est un palindrome"
return 'Ce n\'est pas un palindrome'
|
This part:
1 |
str (monPal.estUnPalindrome(monPal.chaine))
|
does not need to call str() to convert monPal.estUnPalindrome() to a str. It is already a str.
Posts: 201
Threads: 37
Joined: Dec 2021
As usual you're inputs are very useful.
else eliminated and str as well.
Thank you
Posts: 2,125
Threads: 11
Joined: May 2017
You can use a plain function to solve the same problem.
conditional-expressions-pythons-ternary-operator
1 2 |
def est_un_palindrome(chaine):
return "C'est un palindrome" if chaine[:: - 1 ] = = chaine else 'Ce n\ 'est pas un palindrome'
|
But in this example, the lines are too long. This is not readable.
Putting this snippet into the black auto formatter, gives me this result:
1 2 3 4 5 6 |
def est_un_palindrome(chaine):
return (
"C'est un palindrome"
if chaine[:: - 1 ] = = chaine
else "Ce n'est pas un palindrome"
)
|
If you have a str , you can reverse it with slicing.
This is also true for other sequences like tuple , list , dict (the insertion order is saved) but for example not things like frozenset or set . A set or frozenset do not have an order.
1 2 |
a_str = "anna1"
reversed_str = a_str[:: - 1 ]
|
|