Issue
For context, I am using: Windows 10 Jupter Notebook 6.4.12 Python 3.8.13
Question I am trying to understand why the 'if x in else' statement isn't working in my Jupyter Notebooks.
Here is the code I am trying to get to work... but the code keeps returning the statement, stipulating 'sunny' or 'lovely' have been found in the string. However, you can clearly see the string does not contain these words...
Here is the code:
sitrep = "it is horrible weather"
print("The sitrep is: " + sitrep)
print(type(sitrep))
if "sunny" or "lovely" in sitrep:
print("sunny or lovely have been detected")
else:
print("neither has been detected")
Here is the response:
it is horrible weather
<class 'str'>
sunny or lovely have been detected
Incorrect response So, instead of printing 'neither has been detected', it prints 'sunny or lovely have been detected' - which as you can see is wrong, given the if else statement conditions...
Any advice appreciated. Thanks in advance
Solution
The main error is 'or' is not used like that. Rather it should be
if "sunny" in sitrep or "lovely" in sitrep:
print("sunny or lovely have been detected")
else:
print("neither has been detected")
Also print statement should be comma. A space is added for each comma statement
print("The sitrep is:", sitrep)
Answered By - surge10
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.