0

I'm doing a basic programming problem in regards to decision statements. The code should relate to three types of fish with different options whenever each option is inputted. I think most of my code runs properly, except I'm not sure how to format the final, catch-all else that absorbs all incorrect inputs.

My current code works well but the else statement at the bottom is tacked onto every response I give outside of the first input in the solution.

if fish_type == "carnivorous":
    fish_size = str(input("Do you have smaller fish already? "))
    if fish_size == "yes":
        print("This is a bad idea! It'll eat the little ones!")
    if fish_size == "no":
        print("Great! Enjoy!")
if fish_type == "salt water":
    print("Wow, you're a fancy fish parent!")
if fish_type == "community":
    fish_number = int(input("How many fish of this species do you already have?\
 "))
    if fish_number < 3:
        print("You should get more than one fish!")
    else:
        print("Yay, more friends!")
else:
    print("I don't think that's a type of fish; maybe you're looking for a \
lizard?")

For example, if I input "carnivorous" I am routed correctly to the carnivorous if statements, but when I answer "yes" or "no" my else statement is formatted incorrectly. Thanks for your help!

8
  • Do you need 3 ifs for 3 fish_type? Do you meant to use elif for salt water and community? Commented Sep 20, 2019 at 5:07
  • Also, can someone explain the use of if name == 'main': as a formatting thing? Commented Sep 20, 2019 at 5:07
  • the second check should be elif or else. You seem to have multiple if's instead Commented Sep 20, 2019 at 5:09
  • @kuro I think I could have used elif in order to straighten up the code Commented Sep 20, 2019 at 5:09
  • For the second query look at stackoverflow.com/questions/419163/what-does-if-name-main-do Commented Sep 20, 2019 at 5:10

1 Answer 1

1

Your issue may have been the way the print statements were formatted. The below code works.

fish_type = 'not_a_fish'

if fish_type == "carnivorous":
    fish_size = str(input("Do you have smaller fish already? "))
    if fish_size == "yes":
        print("This is a bad idea! It'll eat the little ones!")
    elif fish_size == "no":
        print("Great! Enjoy!")
elif fish_type == "salt water":
    print("Wow, you're a fancy fish parent!")
elif fish_type == "community":
    fish_number = int(input("How many fish of this species do you already have?\n"))
    if fish_number < 3:
        print("You should get more than one fish!")
    else:
        print("Yay, more friends!")
else:
    print("I don't think that's a type of fish; maybe you're looking for a \n lizard?")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.