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 trial

Python

Improper deployment of the 'or' argument.

Hi, all. I wrote a simple code that to practice this lesson and some previous ones but my deficiency is obvious. In the code (shown below), I employed multiple levels of branching. But, even though running the code does not return an error, it still does not accomplish my goal. I think I may be misusing the 'or' argument in line 15. Please, take a look at my code first before reading my question below.

My line 15 reads thus:

   elif A2 == fruit_choice2 or fruit_choice3:

I want to restrict this 'elif' to apply only when A2 (i.e the input for the choice of fruit in line 11) is either fruit_choice2 (i.e. "banana") or fruit_choice3 (i.e. "pear"). That is why I use the 'or' argument. If this code were written perfectly, only an input of "banana" and "pear" for A2 would trigger the printing of lines 16-18. In this ideal case, if I entered "carrot" for A2, python should print lines 20-22. Sadly, when I enter any input besides "apple", lines 16-18 are processed. This is not my intention. I believe the problem might be in how I employed the 'or' argument in line 15. How can I write it to accomplish my goal? How can I restrict the 'elif' to activate when only A2 is "banana" or "pear" only? Let me know if my question needs further clarification. And, if you understand it, please proffer a solution. Thank you.

# Write a code that encourages people to eat one apple each day but has a positive bias towards bananas and pears.

fruit_choice1 = "apple"
fruit_choice2 = "banana"
fruit_choice3 = "pear"


A0 = input("What is your name? ")
A1 = input("Do you eat a fruit serving each day, {}? (Y/N) ".format(A0))
if A1 == "Y":
    A2 = input("What fruit do you eat each day? (Enter a singular noun in lowercase, please) ")
    if A2 == fruit_choice1:
        print("Great choice, {}! Eating one {} a day is recommended.".format(A0,A2))
        print("Have a great {}-eating day today!".format(A2))
    elif A2 == fruit_choice2 or fruit_choice3:
      print("Good. Eating one {} a day is recommended, {}.".format(A2,A0))
      print("You can also eat one {} a day.".format(fruit_choice1))
      print("Have a great day, {}!".format(A0))
    else:
        print("Eating a fruit, including {} (if it is a fruit *wink*) is recommended for each day.".format(A2))
        print("You can also try eating a {} each day, {}.".format(fruit_choice1,A0))
        print("Have a great day today!")
elif A1 == "N":
    print("You should eat a fruit serving each day, {}.".format(A0))
    print("Why not try one {} today!".format(fruit_choice1))
    print("Have a great {}-eating day!".format(fruit_choice1))
else:
    print("I'm sorry I don't understand your response, {}. Please start all over.".format(A0))

2 Answers

boi
boi
14,241 Points

So, you want to restrict line 15 of you're code to accept only "banana" or "pear"?. If that's the case, try this;

fruit_choice1 = "apple"
fruit_choice2 = "banana"
fruit_choice3 = "pear"


A0 = input("What is your name? ")
A1 = input("Do you eat a fruit serving each day, {}? (Y/N) ".format(A0))
if A1 == "Y":
    A2 = input("What fruit do you eat each day? (Enter a singular noun in lowercase, please) ")
    if A2 == fruit_choice1:
        print("Great choice, {}! Eating one {} a day is recommended.".format(A0,A2))
        print("Have a great {}-eating day today!".format(A2))
    elif A2 == fruit_choice2 or A2 == fruit_choice3:
        print("Good. Eating one {} a day is recommended, {}.".format(A2,A0))
        print("You can also eat one {} a day.".format(fruit_choice1))
        print("Have a great day, {}!".format(A0))
    else:
        print("Eating a fruit, including {} (if it is a fruit *wink*) is recommended for each day.".format(A2))
        print("You can also try eating a {} each day, {}.".format(fruit_choice1,A0))
        print("Have a great day today!")
elif A1 == "N":
    print("You should eat a fruit serving each day, {}.".format(A0))
    print("Why not try one {} today!".format(fruit_choice1))
    print("Have a great {}-eating day!".format(fruit_choice1))
else:
    print("I'm sorry I don't understand your response, {}. Please start all over.".format(A0))

Tage me again if this did not solve your problem.

boi , it worked. Thank you very much.

I see the difference between what we both did, but can you explain why mine was wrong, please? Thanks!

boi
boi
14,241 Points
elif A2 == fruit_choice2 or fruit_choice3: 👈#This is an invalid syntax

elif A2 == fruit_choice2 or A2 == fruit_choice3: 👈#This is valid syntax

Your use of or operator was not right.

boi
boi
14,241 Points

pssst... if you mark the "best answer" I'd get extra points, just letting you know 😜