Python Forum
if, or, in, else in 1 line - how to make it work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if, or, in, else in 1 line - how to make it work?
#1
hi guys,

i would like to learn how to make "OR" function in sentence below:
My goal is to get returned Blue if found substring "Blue" or "blue" in string

color= "Blue" if 'blue' in description else "yellow"
i tried to do:


color= "Blue" if 'blue' or 'Blue' in description else "yellow"
but it doesn't work

#edit
I figured it out:
color= "Blue" if 'Blue' in description or "blue" in description else "yellow"
Reply
#2
color= "Blue" if 'blue' in description.lower() else "yellow"
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
thanks for your solution :) good to know about .lower()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 767 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 674 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,316 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Make my py script work only on 1 compter tomtom 14 3,815 Feb-20-2022, 06:19 PM
Last Post: DPaul
  How to make for loop display on 1 Line Extra 3 1,421 Jan-12-2022, 09:29 PM
Last Post: Extra
  How to make input come after input if certain line inserted and if not runs OtherCode Adrian_L 6 3,319 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
  Cannot make 'pandas' module to work... ellie145 2 4,183 Jan-05-2021, 09:38 PM
Last Post: ellie145
  Is there anyway to make this work? dre 3 2,151 Nov-26-2020, 12:40 PM
Last Post: jefsummers
  Line of code to show dictionary doesn't work MaartenRo 2 2,420 Jul-28-2020, 03:58 PM
Last Post: deanhystad
  Cannot Make the python Code work ErnestTBass 4 2,660 Apr-23-2020, 02:42 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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