'Python's PRINT is not outputting the last print() word
My code runs perfectly and with the results that I want. However, the last print ("Anxious: ") is NOT outputting Anxious: but instead only printing out the answer without the title.
I have run this multiple times in IDLE 3.10.1 as well as my Python I online class's own Python program and I get the exact same result. Absolutely no errors but no title either after the last print statement.
Does anyone have an idea of what is going on? I have not had this kind of problem before in my previous code that I ran for my online class.
Thank you!
busy = True
hungry = False
tired = True
stressed = False
happy = busy and not stressed
sad = hungry or tired
print("Happy: " + str(happy))
print("Sad: " + str(sad))
print("Confused: "+ str(happy and sad))
print("Bored: " + str(not(happy or sad or busy)))
print("Anxious: " + str(not happy) and (not sad) and (not stressed))
Solution 1:[1]
The line
print("Anxious: " + str(not happy) and (not sad) and (not stressed))
evaluates to
print("Anxious: False" and False and True)
Strings always evaluate to True, so you are printing the result of True and False and True which simply prints False.
Solution 2:[2]
Its not working because the paranthesis for str is not covering the entire operation it is just covering the not happy part of the operation, Thus, all you have to do is to add a new paranthesis to it
print("Anxious: " + str((not happy) and (not sad) and (not stressed)))
Solution 3:[3]
Yes, the last code you made a mistake. First, you string the word true, and then you use the logical operator And.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RedCocoa |
| Solution 2 | MK14 |
| Solution 3 | Dharman |
