This search function works but if I for example search for an animal called Sarah and there are two animals in the list called Sarah it only prints one of them. How should I do to make it print all the search matches?
def search():
choice = int(input("""
1 Name
2 Age
3 Specie
4 Gender
What do yo want to search for? """))
if choice == 1:
your_search = input("Write the animal name: ").capitalize()
for Animals in animal_list:
if your_search == Animals.name:
print(Animals)
break
else:
print("Sorry, couldn't find any animal called " + your_search + ", maybe check the spelling.")
Try removing break after each successful match
You're telling the program to stop searching after it finds a result. Just remove the break command and add a marker stating if a name was found:
def search():
choice = int(input("""
1 Name
2 Age
3 Specie
4 Gender
What do yo want to search for? """))
if choice == 1:
your_search = input("Write the animal name: ").capitalize()
found = False
for Animals in animal_list:
if your_search == Animals.name:
found = True
print(Animals)
if found == False:
print("Sorry, couldn't find any animal called " +
your_search + ", maybe check the spelling.")
Related
import random
print(
"Do you think you know all the animals😺😼?! if yes then this game is for \033[1myou!\033[0m")
print("\033[7m""Let the game begin! ""\033[0m")
animals = ("ant baboon badger bat bear beaver camel cat clam cobra cougar").split()
you_chose = []
animal = random.choice(animals)
hint = random.choice(list(animal))
used_hints = []
lives = 6
hint_count = 6
while True:
print()
letter = input("please type a letter to begin: ").lower()
if letter in you_chose:
print("\033[31m Error doublicate letter\033[0m")
continue
else:
you_chose.append(letter)
if letter in animal:
print("Congrats, you found a letter! keep going.")
else:
print("Not in there keep trying")
lives = -1
want_hint = input("Would you like a hint?(yes \ No)\n: ").lower()
if want_hint == "yes":
for want_hint in hint:
if want_hint not in used_hints and want_hint not in you_chose:
hint_count += 1
print(hint, end="")
used_hints.append(want_hint)
else:
continue
for letter in animal:
if letter in you_chose:
print(letter, end="")
else:
print("*", end="")
#question- i need to save the randomised animal which then be turned to a list to be used as hints for the game but as the games in a while true loop the first time we ask for a hint itll give the original animal but next time the hint becomes from cat to dog i need to save the value the random choice gave before turning it into a hint i tried the seed funtion as im new to code thought itll work well didnt
try something like this, this will make the animals string input into a list:
animals = list(animals)
animal = random.choice(animals)
print(animal)
or try:
animals = ['cat','human','dog','bird'...]
animal = random.choice(animals)
print(animal)
the lists mammals, amphibians, and reptiles contain a list of animals
name = input("please input a animal ")
if name in mammals or name in amphibians or name in reptiles:
print (idk what to put here)
else:
print ("it is not there")
Anthony is right that you can't print a variable's name directly, but we can do something like this:
name = input("please input a animal ")
if name in mammals:
print("mammals")
elif name in amphibians:
print("amphibians")
elif name in reptiles:
print("reptiles")
else:
print ("it is not there")
First, this checks if the animal is included in the list mammals. If it is, we print "mammals". We do the same for amphibians and reptiles. If the animal is in neither of those, we print "it is not there".
The only way is to iterate on each list and check if it contains the name, if yes, you can print the list:
name = input("please input a animal ")
for l in (mammals, amphibians, reptiles):
if name in l:
print(l)
else:
print ("it is not there")
I'm stuck with a problem that asks 5 questions. If any of them has the answer no It should print ALIEN! or else Cool.
This is what I have got so far:
human = input("Are you human? ")
human = input("Are you living on planet Earth? ")
human = input("Do you live on land? ")
human = input("Have you eaten in the last year? ")
human = input("Is 2 + 2 = 4? ")
if human == "yes":
print("Cool")
elif human == "no":
print("ALIEN!")`
You could use any() to check if any of the answers to questions is 'no' and print message accordingly:
human = [input("Are you human? "), input("Are you living on planet Earth? "),
input("Do you live on land? "), input("Have you eaten in the last year? "), input("Is 2 + 2 = 4? ")]
if any(x.lower() == 'no' for x in human):
print('ALIEN!')
else:
print('Cool')
You have the variable human that is changed every time and given a new value using input() Try and make multiple variables instead of just using human.
human = input("Are you human? ")
var1 = input("Are you living on planet Earth? ")
var2 = input("Do you live on land? ")
var3 = input("Have you eaten in the last year? ")
var4 = input("Is 2 + 2 = 4? ")
if(human == "yes"):
print("Cool")
elif(human == "no"):
print("ALIEN!")
If you're not worried about storing the variables and only care if one of the questions comes up as "no" or "n":
x=["human?","on earth?", "on land?","someone who eats food?","sure that 2+2=4?"]
for i in x:
if input("Are you {}".format(i)).lower() in ['no','n']:
print("Alien!")
break
else:
print("Cool")
Just a side note: Here you can see a great case for using a for loop since there is code that is repeated many times. Personally, I would do the following to solve this problem.
Create a list of questions
Iterate through the list of questions
If any answer is no, break and print Alien.
Now for the code:
# Initialize our list
lst = ["Are you human? ", "Are you living on planet Earth? ","Do you live on
land? ","Have you eaten in the last year? ","Is 2 + 2 = 4? "]
#Create a flag for Alien
flag = False
#Iterate through the list
for question in lst:
answer = input(question)
if answer == 'no':
#Print alien
print('Alien')
#Change Flag
flag = True
#Break out of the loop
break
#Check to see if our flag was thrown during the loop
if flag == False:
print('cool')
If you want more help on solving coding challenges like this one, check out this intro to python course: https://exlskills.com/learn-en/courses/learn-python-essentials-for-data-science-intro_to_python/content
I am trying to complete two different questions but cannot get them to work. Please help me understand where I went wrong.
1) For each number between 1 and 100, odds should be normal and even numbers should print out the word "Billy". Must start at 1 not 0 and include the number 100. Here's my answer (I know I'm way off)
for i in range(1,101):
if i % 2 == 0:
print(Billy)
else:
print(i)
2) Ask the user: "What is your name?". Response should look like "Hello Billy" for all names except Joe and Susie. For Joe it should say "Hi Joe :)" and for susie it should say "Ahoy Susie :D". Here is where I'm at:
name = input("What is your name?")
if name == "Joe":
print("Hi Joe :)")
if name == "Susie":
print("Ahoy Susie :D)
else: print("Hello", name)
try this
for i in range(1,101):
if i % 2 == 0:
print('Billy') #you missed quote marks here
else:
print(i)
(bad indentation, and missing quote marks)
and
name = input("What is your name?")
if name == "Joe":
print("Hi Joe :)")
elif name == "Susie":
print("Ahoy Susie :D") #and you missed quote marks here
else:
print("Hello" + name)
...same issues.
I have everything right in my code (I think) except the part where I get the correct names from my dictionary.
My code is:
studentdirectory = {"Andrew": ["Jane", "John"], "Betsy": ["Ellen", "Nigel"], "Louise": ["Natalie", "Louis"], "Chad": ["Mary", "Joseph"]}
def menu():
print
print ("Enter 1 to retrieve the mother's name of the child.")
print ("Enter 2 to retrieve the father's name of the child.")
print ("Enter 3 to retrieve the name of both parents of the child.")
print ("Enter 0 to quit.")
print
while True:
choice = input("Enter your choice now: ")
if (choice >= 0) and (choice<= 3) and (int(choice) == choice):
return choice
else:
print ("Your choice is invalid. Please try again with options 0 to 3.")
for key in studentdirectory:
mom = studentdirectory[key][0]
dad = (studentdirectory[key][1])
def main():
while True:
choice = menu()
if choice == 0:
break
else:
name = raw_input("Enter the name of the child: ")
if studentdirectory.has_key(name):
if choice == 1:
print "The name of the child's mother is ", mom, "."
elif choice == 2:
print "The name of the child's father is ", dad, "."
else:
print "The name of the child's parents are ", mom, " and ", dad, "."
else:
print "The child is not in the student directory."
main()
I would like to keep my code as close to this as possible. I just need help in understanding how to get separate values in the dictionary, because right now for every mom and dad I only get Louise's parents back. How do i fix this??
This is Python Language.
You're getting the values of mom and dad in a loop, but overwriting them each time, so they're always set to the value of the last loop cycle (Louise in your case). You should define them when you need them only instead:
def main():
while True:
choice = menu()
if choice == 0:
break
else:
name = raw_input("Enter the name of the child: ")
if studentdirectory.has_key(name):
mom = studentdirectory[name][0]
dad = studentdirectory[name][1]
if choice == 1:
print "The name of the child's mother is ", mom, "."
elif choice == 2:
print "The name of the child's father is ", dad, "."
else:
print "The name of the child's parents are ", mom, " and ", dad, "."
else:
print "The child is not in the student directory."
if studentdirectory.has_key(name):
mom = studentdirectory[key][0]
dad = (studentdirectory[key][1])
And delete the for key in studentdirectory loop part
Because when you get a student name in the main loop.Your original code only return a const mom and dad varible,which comes from your for loop above the main() defination.
And logically
You can only get the parents name after you have the child's name