Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTiago Ramos
2,380 Pointsstuck in the question
hello, I think i've made my conditions well. Somebody understands where my problem is? thank you :)
import sys
# ask user to start the movie
user_choice = input("Do you want to start the movie? Y/n ")
# start the movie if user answer isn't "n" or "N"
def start_movie(user_choice):
# if user answer isn't "n" or "N" start the movie
if user_choice != "n" or user_choice != "N":
print("Enjoy the show!")
# otherwise exit
else:
sys.exit()
2 Answers
Krishna Pratap Chouhan
15,203 PointsTwo problems!!! First is you haven't called the function, even though you defined it.
Second is a little problem in the if condition: what you are checking is: if user_choice is 'n' and also user_choice is 'N' ... only then exit.
Little complicated! :) its like negative of negative is positive.
Here is the code... try this and you'll understand better practically.
import sys
# ask user to start the movie
user_choice = input("Do you want to start the movie? Y/n ")
# start the movie if user answer isn't "n" or "N"
def start_movie(user_choice):
# if user answer isn't "n" or "N" start the movie------------------>check AND in line below.
if user_choice != "n" and user_choice != "N":
print("Enjoy the show!")
# otherwise exit
else:
sys.exit()
start_movie(user_choice)
Oszkár Fehér
Treehouse Project Revieweruser_choice.lower().....it's cleaner and shorter and you DRY. and than you don't need the AND... my opinion...
Tiago Ramos
2,380 PointsTiago Ramos
2,380 Pointsthanks Krishna, I understand that I haven't called the function, but in the condition I don't understand why should I use "and" instead of "or", because the user can answer "n" or "N"
Krishna Pratap Chouhan
15,203 PointsKrishna Pratap Chouhan
15,203 PointsTiago Ramos , You are right... user can answer "n" or "N" for "NO" but he can't answer "n" and "N" for "YES".
There are three ways to understand this, first 2 are complicated but the last is easy one and i prefer.
. First... lets see the original condition statement.
if user_choice != "n" or user_choice != "N":
Now the question says...if choice is not equal to 'No' print something. Perfectly correct score!.
But we have divided this condition into two parts: (choice!='n') , (choice!='N').
Now BOTH of these condition means "YES" but EITHER of these condition not necessarily means "YES". . .
Second...
So if either of the condition(choice=='n' , choice=='N') satisfies it's a "NO".
But that does not means that either of these condition fails means a "YES".
Rather when both these conditions fails then only it means "YES".
All too complicated, right.
Third...
The best way to verify this(i like the most) is through test cases.
According to original code:
choice!='n' OR choice!='N' => YES
Here, lets see if value is : 1)n, 2)N, and 3)L ..... satisfying in the code:
1) 0 + 1==> yes
2) 1 + 0==>Yes
3) 1 + 1 ==> Yes
Lets consider an imaginary case where the value is both 'n' and 'N' at the same time.
0 + 0 ==>No.
I hope this was helpful.