Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Conditionals
#1
Hi All Big Grin

I am new to python. I am doing this tutorial on nested conditions coding however I am stuck. I appreciate some help with it.

if already_fed:
    print('Already fed!')
else:
    if animal == 'Lion':
        print('Meat')
    elif animal == 'Zebra':
        print('Grass')
    else:
        print('Water')
I want to set already_fed to False and animal to 'Warthog' so that "Water" is printed to the terminal. i always ended with result as already fed.
Larz60+ write Jul-27-2021, 03:11 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
Hi shen123,

the following worked for me, is this the type of thing, you were thinking of ? :-

animal = ['Lion', 'Zebra', 'Warthog', 'Elephant', 'Tiger', 'Hyena']

animal = 'Warthog'

if animal == 'Lion':
        print('Meat')
elif animal == 'Zebra':
        print('Grass')
elif animal == 'Warthog':
        print('Water')
elif animal in ['Elephant', 'Tiger', 'Hyena']:
    print('Already fed!')
else:
    print('Animal not listed')

print()
If not, could you post the Full Code you have ?

Best Regards

Eddie Winch
Reply
#3
What is already_fed? Guessing it's a variable set to True.
Here is one way using a dict

#! /usr/bin/env python3

animals = {
'Lion': ('meat', True),
'Zebra': ('grass', True),
}

animals['Warthog'] = ('grain', False)

for animal, already_fed in animals.items():
    if already_fed[1]:
        print(f'{animal} has been fed {already_fed[0]}')
    else:
        print(f'{animal} has not been fed. {animal} needs water')
Output:
Lion has been fed meat Zebra has been fed grass Warthog has not been fed. Warthog needs water
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
Setting already_fed to False and animal to 'Warthog', "Water" is printed, I'm not sure how you ended with result as 'Already fed!'.
already_fed = False
animal = 'Warthog'

if already_fed:
    print('Already fed!')
else:
    if animal == 'Lion':
        print('Meat')
    elif animal == 'Zebra':
        print('Grass')
    else:
        print('Water')
Output:
Water
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Doubt about conditionals in Python. Carmazum 6 1,591 Apr-01-2023, 12:01 AM
Last Post: Carmazum
  conditionals based on data frame mbrown009 1 891 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Efficiency with regard to nested conditionals or and statements Mark17 13 3,152 May-06-2022, 05:16 PM
Last Post: Mark17
  Nested conditionals vs conditionals connected by operators dboxall123 8 3,038 Feb-18-2022, 09:34 PM
Last Post: dboxall123
  Invalid syntax using conditionals if - else jperezqu 1 2,322 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  conditionals with boolean logic?? ridgerunnersjw 3 1,984 Sep-26-2020, 02:13 PM
Last Post: deanhystad
  two conditionals with intermediate code Skaperen 5 2,756 Jul-12-2020, 07:18 PM
Last Post: Skaperen
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,963 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  Nested Conditionals HELP absolum 4 2,766 Jul-17-2019, 06:40 PM
Last Post: absolum
  Branching and Conditionals/If statements MSL 1 1,924 Jan-26-2019, 11:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020