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!

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
DysDiv
  • 7
  • 6
  • Do you need 3 `if`s for 3 `fish_type`? Do you meant to use `elif` for `salt water` and `community`? – kuro Sep 20 '19 at 05:07
  • Also, can someone explain the use of if __name__ == '__main__': as a formatting thing? – DysDiv Sep 20 '19 at 05:07
  • the second check should be `elif` or `else`. You seem to have multiple `if`'s instead – moys Sep 20 '19 at 05:09
  • @kuro I think I could have used elif in order to straighten up the code – DysDiv Sep 20 '19 at 05:09
  • For the second query look at https://stackoverflow.com/questions/419163/what-does-if-name-main-do – kuro Sep 20 '19 at 05:10
  • Thank you, I think replacing the two "if"s with "elif"s solved the problem. – DysDiv Sep 20 '19 at 05:11
  • if you have only 2 possibilities, use `if` to chek one & use `else` after wards. If there are multiple possibilities, use `if`,`elif`,`elif`...till the last possibility that you want to check & use `else` for the rest – moys Sep 20 '19 at 05:12
  • You have 2 `if`s inside fish size also. I think you can just use `if` & `else` there since the options are just `yes` & `no`. When you use `else`, you don't even have to state `fish_size == "no"` – moys Sep 20 '19 at 05:13

1 Answers1

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?")
ParalysisByAnalysis
  • 703
  • 1
  • 4
  • 16