Python Forum
If, elif, else doesn't work well
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If, elif, else doesn't work well
#1
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?
Reply
#2
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
Reply
#3
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.
Reply
#4
Larz:
I don't understand,
as i put everything in uppercase, the answer "T" is True in the first method.

Paul
Reply
#5
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")
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 870 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,681 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 842 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,655 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,054 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,930 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,660 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Process doesn't work but Thread work ! mr_byte31 4 2,556 Oct-18-2021, 06:29 PM
Last Post: mr_byte31
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,888 Aug-10-2021, 07:00 AM
Last Post: ndc85430
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,647 Jul-17-2021, 04:19 PM
Last Post: bill_z

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020