Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Conditionals
#1
Hello Forum!

I am new here and also at Python. I am currently learning Python through archived material on edx (a Microsoft Learning Platform). My current challenge is a task involving Nested Conditionals. Essentially it is code using nested if statements in a guessing application. I am trying to use user prompts to let users make a maximum of 3 guesses from a variable containing bird names.

The first part of the code (the part with nested if statements) never runs. Only the else part seems to execute. Kindly tell me what I am doing wrong. Find the task and code below.
Thank you.


Task
Nested if - testing for False
Program: [ ] 3 Guesses

use nested if statements complete the flowchart code
create a birds string variable with the names of 1, 2, 3 or more birds to make it easier
get bird_guess input and use bird_guess in bird_names to generate Boolean True/False
if the the guess is wrong (False) create a sub test until the user has had 3 guesses



My Code
# [ ] Create the "Guess the bird" program
print("Welcome to the Bird Guess Game!")
birds = ("Pigeon, Eagle, Parrot, Chicken, Turkey, Macaw, Duck, Geese")
# print(birds)
print()
bird_guess = input("Name a bird you think is on the bird list: ")

if bird_guess.title() in birds == False:
print("Wrong! You have 2 more guesses.")
next_guess = input("Name a bird you think is on the bird list: ")

if next_guess.title() in birds == False:
print("Wrong! You have 1 more guess.")
last_guess = input("Name a bird you think is on the bird list: ")

if last_guess.title() in birds == False:
print("Wrong! Sorry you are out of guesses.")
else:
print("Correct! You got it on your 3rd and last try.")
else:
print("Correct! You got it on your 2nd try.")
else:
print("Correct! You got it on your 1st try.")
Reply
#2
Hello and welcome to Python and the forum!
In future please put your code in Python code tags, you can find help here. It will make your code readable and thus much easier for others to assist.
Reply
#3
(Apr-16-2018, 05:19 PM)Elero Wrote: birds = ("Pigeon, Eagle, Parrot, Chicken, Turkey, Macaw, Duck, Geese")
Ok, so that's not a list, or tuple, of birds. It's a single string.
If you fix that, the rest of your code might work.

(Apr-16-2018, 05:19 PM)Elero Wrote: if last_guess.title() in birds == False:
This is a bit of a nitpick, but there's no reason to have the == False there. The in operator already returns True/False, so it's redundant, and makes the code a little harder to read.
Reply
#4
(Apr-16-2018, 06:02 PM)j.crater Wrote: Hello and welcome to Python and the forum!
In future please put your code in Python code tags, you can find help here. It will make your code readable and thus much easier for others to assist.


Thank you. I am currently trying to figure out the tags.

(Apr-16-2018, 06:11 PM)nilamo Wrote:
(Apr-16-2018, 05:19 PM)Elero Wrote: birds = ("Pigeon, Eagle, Parrot, Chicken, Turkey, Macaw, Duck, Geese")
Ok, so that's not a list, or tuple, of birds. It's a single string.
If you fix that, the rest of your code might work.

(Apr-16-2018, 05:19 PM)Elero Wrote: if last_guess.title() in birds == False:
This is a bit of a nitpick, but there's no reason to have the == False there. The in operator already returns True/False, so it's redundant, and makes the code a little harder to read.

Awesome! I made the birds variable a list and removed the
== false
and it executed perfectly. I will study it and try and understand why it did not work.

Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions with conditionals and comparison operators Drone4four 9 12,903 Jan-01-2019, 06:48 PM
Last Post: Drone4four
  Error with conditionals RiceGum 17 7,168 Oct-18-2017, 01:45 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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