Python Forum
If, elif, else doesn't work well - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: If, elif, else doesn't work well (/thread-31080.html)



If, elif, else doesn't work well - Vidar567 - Nov-21-2020

print("Draw it?")
chart = str(input())

if chart == "Tak" or "tak" or "t":

    print("Draw chart)

elif chart == "Nie" or "nie" or "n":
    print("You chose to ...")
else:
    print("inappropriate choice.")
My else, elif doesn't work well.
When I put input 'sasd' it goes for drawing a chart, when I input 'Nie' it goes for drawing a chart.

How to fix it, do you have some suggestions?


RE: If, elif, else doesn't work well - DPaul - Nov-21-2020

Hi,
Your "or" should contain the variable each time, like so:
if chart == "Tak" or chart =="tak" or chart=="t":
You might also add a second quote to print("draw chart").
Then it will be fine,although there are other ways to do this, eg.
if chart.upper() in ['TAK','T']:
Paul


RE: If, elif, else doesn't work well - Larz60+ - Nov-21-2020

DPaul,
Your first answer is correct, but the second suggestion is not.
if for example the input is Originally 'T' it would be False by first method, but True by the second.
the first ( False ) is correct.


RE: If, elif, else doesn't work well - DPaul - Nov-21-2020

Larz:
I don't understand,
as i put everything in uppercase, the answer "T" is True in the first method.

Paul


RE: If, elif, else doesn't work well - deanhystad - Nov-21-2020

TAK is not True in the first method, not does it appear to be a a desired True outcome when looking at the original post.

I find it odd being so concerned about a Yes/No (Tak/Nie) response. I ignore everything but the first letter and don't care about invalid responses.
chart = input("Draw it (y/n): ")  # Provide clue
if chart and chart[0] in 'Yy':  # Only looking at first letter
    print("Draw chart")



RE: If, elif, else doesn't work well - DPaul - Nov-21-2020

Quote:TAK is not True in the first method, not does it appear to be a a desired True outcome when looking at the original post.

It was only a suggestion to use chart.upper().
Now other variations are covered such a 'TAk', 'tAK', N,Nie,NIe,...etc.
Because there seems to be only one Drawing, it's a valid alternative.
There are 3 messages, so looking only at 'Yy' won't cover that, but it is elegant.
Paul