Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guessing game problem
#1
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
Reply
#2
See https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#3
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]}')
Reply
#4
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.
Reply
#5
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}: ")
Reply
#6
Thx guys it helped! Smile
Reply
#7
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.
Reply
#8
thx guys go to this link to play the full game Clap : https://trinket.io/library/trinkets/bb0249b6cd
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stacking Problem in a game. HelloTobi22 2 960 Aug-05-2022, 09:48 AM
Last Post: HelloTobi22
  Problem with my pong game code Than999 8 3,761 May-15-2022, 06:40 AM
Last Post: deanhystad
Question Beginner Boolean question [Guessing game] TKB 4 2,227 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Unable to count the number of tries in guessing game. Frankduc 7 1,847 Mar-20-2022, 08:16 PM
Last Post: menator01
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,502 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,682 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Python Hangman Game - Multiple Letters Problem t0rn 4 4,536 Jun-05-2020, 11:27 AM
Last Post: t0rn
  Python Help - Guessing Game JamieT 5 2,684 Apr-16-2020, 01:30 PM
Last Post: deanhystad
  game of the goose - dice problem koop 4 3,410 Apr-11-2020, 02:48 PM
Last Post: ibreeden
  Guessing game kramon19 1 2,132 Mar-25-2020, 04:17 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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