Python Forum
if and condition not working for string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if and condition not working for string
#1
I have three list

out5 = ['increasing', 3, 5,3]

out50 = ['increasing', 9, 5,3]

out95 = ['increasing', 8, 7,3]
I am trying to create this condition

if out5[0]=='increasing' & out50[0] == 'increasing' & out95[0] == 'increasing':
       das = 2
but I get error
Error:
Traceback (most recent call last): File "/Users/mada0007/PycharmProjects/Research_ass/station_data_analysis.py", line 240, in <module> if out5[0] == 'increasing' & out50[0] == 'increasing' & out95[0] == 'increasing': TypeError: unsupported operand type(s) for &: 'str' and 'str'
How can I please overcome this?
Reply
#2
To overcome such problems you should learn to read error messages: unsupported operand type(s) for &: 'str' and 'str'

In conditionals you must use 'and' (or 'or') instead of '&'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
thanks for reply, that didnt come to mind because I used the same '&' for the same if condition in a different part of my code and it did work.

But thanks though
Reply
#4
'&' is 'bitwise AND' but you need 'boolean AND'. You can learn operators and their precedences from official documentation: 6. Expressions >>> 6.16. Operator precedence

If '&' works in your code make sure that it does what you expect it to do.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Thanks!!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last caracter of a string truncated issue when working from the end of the string Teknohead23 3 1,598 Oct-03-2021, 01:08 PM
Last Post: snippsat
  else condition not called when if condition is false Sandz1286 10 5,913 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 8,023 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Differential equations with initial condition in Python (change a working code) Euler2 1 1,844 May-29-2020, 04:06 PM
Last Post: Euler2
  Converting query string as a condition for filter data. shah_entrance 1 1,806 Jan-14-2020, 09:22 AM
Last Post: perfringo
  F-String not working when in IDLE editor nadimsarrouh 5 5,911 Jan-29-2019, 10:02 AM
Last Post: nadimsarrouh

Forum Jump:

User Panel Messages

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