Python Forum

Full Version: Guessing game problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys,

Im kinda new to python programming but know most of the basics. Currently im using a platform named trinket.io .
Im trying to make an animal guessing game. My program is supposed to ask the user for a certain category of animals. For instance, Savanna, Tundra, Desert, and Tropical. I've made 4 lists of 5 animals each for the user to choose from based on which biome they choose. Anyway, whenever I type in tundra, it always shows the savanna list. Please reply if you know what is wrong(sorry if im using this wrong, im new to this forum).
Here is my code(i have only done the conditions for tundra and savanna):

import random
savanna=['cheetah','lion','gazelle','wildebeest','elephant']
tundra=['artic fox','artic hare','lemming','muskox','rock ptarmigan']
tropical=['jaguar','sloth','gorrila','poison dart frog','toucan']
desert=['cactus wren','black-tailed jackrabbit','kangaroo rat','xerocole','camel']
lives=3



while True:
  category=input("pick a category of animals. Savanna, Tundra, Tropical, or Desert:")
  if category=='Savanna' or 'savanna':
    savannaRandom=random.randint(0,4)
    chosenAnimal=savanna[savannaRandom]
    while (lives>0 and (category=='Savanna' or 'savanna')):
      print 'You have ',lives,' lives'
      guess=input("Guess the animal between cheetah, lion, gazelle, wildebeest, or elephant-")
      if guess==chosenAnimal:
        print "you win!"
        break
      else:
        print 'Try again'
        lives=lives-1
  elif category=='Tundra' or 'tundra':
    tundraRandom=random.randint(0,4)
    chosenAnimal1=tundra[tundraRandom]
    lives=3
    while lives>0:
      print 'You have ',lives,' lives'
      guess1=input("Guess the animal between artic fox, artic hare, lemming, muskox, or rock ptarmigan-")
      if guess1==chosenAnimal1:
        print "you win!"
        break
      else:
        print 'Try again'
        lives=lives-1
You might want to target python3 for new code. Python2 is very, very old at this point.

Your problem is on line 12. You're reading this as Is the category either of these options. But python is parsing this as: if (category == "Savanna") or ("savanna")

In this case the second option ("savanna") will always be interpreted as true. To see if it's one of several options, instead use in.

if category in ("Savanna", "savanna"):
Of course if the only reason you're doing the multiple checks is because of the casing, a common way to do this is to compare against the lower_cased version. That way any casing that the person types in would be accepted...

if category.lower() == "savanna":
Also, you're typing in the list of animals twice, once at the beginning, and again in the input. You can have the input generate your list instead..
guess = input(f'Guess the animal between {", ".join(savanna[:-1])} or {savanna[-1]}')
What Yoriz said. Smile Also, you should strongly consider using Python 3 rather than Python 2, especially as a new user. Python 2 received its final release earlier this year and is now officially no longer supported.
This does not do what you think it does
if category=='Savanna' or 'savanna':
This is actually doing:
if (category=='Savanna') or ('savanna' != ''):
'savanna' is not a blank string, so the category is always savanna.

Your category selection would be a lot cleaner as a dictionary.
savanna=['cheetah','lion','gazelle','wildebeest','elephant']
categories = {
    tundra: ['artic fox','artic hare','lemming','muskox','rock ptarmigan'],
    tropical: ['jaguar','sloth','gorrila','poison dart frog','toucan'],
    desert: ['cactus wren','black-tailed jackrabbit','kangaroo rat','xerocole','camel']}

while True:
    inp = input("pick a category of animals. Savanna, Tundra, Tropical, or Desert:")
    category = categories.get(inp.capitalize())
    if category is None:
        print("Please select a valid category.")
        continue
    choice = random.choice(category)
    inp = input(f"Guess the animal from {category}: ")
Thx guys it helped! Smile
import random
savanna=['cheetah','lion','gazelle','wildebeest','elephant']
tundra=['artic fox','artic hare','lemming','muskox','rock ptarmigan']
tropical=['jaguar','sloth','gorrila','poison dart frog','toucan']
desert=['cactus wren','black-tailed jackrabbit','kangaroo rat','xerocole','camel']
lives=3
 
while True:
    situation = input("pick a category of animals. Savanna, Tundra, Tropical, or Desert:")
    if situation in('savanna','Savanna'):
        savannaRandom=random.randint(0,4)
        chosenAnimal=savanna[savannaRandom]
        while (lives>0 and (situation =='Savanna' or 'savanna')):
            print ('You have ',lives,' lives')
            guess=input("Guess the animal between cheetah, lion, gazelle, wildebeest, or elephant-")
            if guess==chosenAnimal:
                print ("you win!")
                break
            else:
                print ('Try again')
                lives=lives-1
    elif situation in ('tundra','Tundra'):
        tundraRandom=random.randint(0,4)
        chosenAnimal1=tundra[tundraRandom]
        lives=3
    while lives>0:
        print ('You have ',lives,' lives')
        guess1=input("Guess the animal between artic fox, artic hare, lemming, muskox, or rock ptarmigan-")
        if guess1==chosenAnimal1:
            print ("you win!")
            break
        else:
            print ('Try again')
            lives=lives-1
It works now.
thx guys go to this link to play the full game Clap : https://trinket.io/library/trinkets/bb0249b6cd