Reputation: 33
So as my first project on python I am trying to make a dichotomous key like program where it guesses what animal you are thinking of after asking questions, I'm really new to this so try to explain it kind of simple :). Also sorry if this question has been asked somewhere else, I didn't really know how to ask it.
think=input ("Think of an animal. Type ready when you want to begin")
think=think.upper()
#FUR
if think=="READY" :
fur=input ("Does it have fur?")
else :
print ("I'll be waiting")
if fur=="YES" :
legs=input ("Does it walk on four legs?") :
elif fur=="NO" :
reptile=input ("Is it a reptile?")
#REPTILE
if reptile=="YES" :
shell=input ("Does it have a shell?")
if reptile=="NO" :
fly=input ("Can it fly?")
#LEGS
if legs=="YES" :
pet=input ("Do people own it as a pet?")
if legs=="NO" :
marsupial=input("Is it a marsupial?")
I can't get it to skip to "Do people own it as a pet" if you answer yes on legs. also, the "I'll be waiting" (else) doesn't work. Oh, and this is python 3.x btw.
Edited for formatting
Edit 2: Got rid of the parentheses in my comparisons :)
Upvotes: 3
Views: 9902
Reputation: 125
let's start from the top:
think=input ("Think of an animal. Type ready when you want to begin")
think=think.upper()
#FUR
if think=="READY" :
fur=input ("Does it have fur?")
else :
print ("I'll be waiting")
if user enter anything else except "ready" for the first input which is stored in "think" then if condition will be false and your program straightly will go to else part so in the second part:
if fur=="YES" :
legs=input ("Does it walk on four legs?") :
elif fur=="NO" :
reptile=input ("Is it a reptile?")
it'll crash your program cause there is no variable called fur and you want to compare it with something.
for situation like this(waiting till user enter your expected input) it'll be better to use an infinite loop and when user entered your expecting input get out from that using break.
so you must change first part to:
think=input ("Think of an animal. Type ready when you want to begin")
think=think.upper()
#THINK
while True:
if think=="READY" :
fur=input ("Does it have fur?")
break
else :
print ("I'll be waiting")
for other parts the exact thing as above may occur again( for instance as you mentioned if user say "YES" to "Does it walk on four legs?" again you don't have any variable called reptile which you want to compare it with something else in next lines)
I suggest to use nested conditions:
#FUR
if fur=="YES" :
legs=input ("Does it walk on four legs?")
#LEGS
if legs=="YES" :
pet=input ("Do people own it as a pet?")
elif legs=="NO" :
marsupial=input("Is it a marsupial?")
elif fur=="NO" :
reptile=input ("Is it a reptile?")
#REPTILE
if reptile=="YES" :
shell=input ("Does it have a shell?")
if reptile=="NO" :
fly=input ("Can it fly?")
also don't forget to:
1-clear the : in this part of your code
legs=input ("Does it walk on four legs?") :
2-if you want to get a newline after asking something from user such as in first line, \n must be useful
think=input ("Think of an animal. Type ready when you want to begin\n")
or even can use print for the string(cause print automatically will add a newline after each time you use it):
print("Think of an animal. Type ready when you want to begin")
think=input()
Upvotes: 1