建立一个蟒蛇动物猜谜游戏

时间:2014-11-03 02:04:00

标签: python

我在基础编程课程中,我们必须做一个动物猜谜游戏。基本的想法是创建一个动物和属性的字典,并让用户猜测属性并输出是或否,如果他们猜测动物的名字打印你赢了或再试一次。当用户输入动物的名称或属性并打印正确的响应时,我一直坚持让代码正常工作。任何帮助将非常感激。

import random
animal = {'lion': ['mane', 'teeth', 'pride', 'Africa', 'predator'],
'tiger': ['stripes', 'fur', 'endangered', 'cat', 'claws'],
'bear': ['hibernates', 'North America', 'Brown', 'Fur', 'Strong'],
'owl': ['hoot', 'nocturnal', 'flies', 'big eyes', 'eats mice'],
'frog': ['pond', 'green', 'tongue', 'amphibian', 'eats flies'],
'toucan': ['rainbow', 'long beak', 'South America', 'tropical', 'wings'],
'monkey': ['eats bananas', 'trees', 'tail', 'swing', 'primate'],
'shark': ['ocean', 'dangeous', 'cartilege', 'sharp teeth', 'fins'],
'zebra': ['stripes', 'black and white', 'africa', 'safari', 'hoofs'],
'wolverine': ['vicious', 'skunk bear', 'brown', 'small', 'fast']}

randomanimal = random.choice(dict(enumerate(animal)))
key = random.choice(animal.keys())
values = random.choice(animal.values())
print ("Let's play a game!")
print ("Guess the animal I'm thinking of.")
guess = raw_input('What animal am I thinking of?')
if guess == "value":
    print("yes")
else:
    print("no")
if guess == "key":
    print("You Win!")
else:
    print("Try Again.")

2 个答案:

答案 0 :(得分:1)

animal.keys()为您提供字典中所有键的列表。使用random.choice就可以随机animal(key)。接下来你想要那只动物的values。所以我在随机选择的动物身上使用.get

您是否向用户显示attributes?并基于他猜测?如果是,那么这将有效。

import random

animal = {'lion': ['mane', 'teeth', 'pride', 'Africa', 'predator'],
'tiger': ['stripes', 'fur', 'endangered', 'cat', 'claws'],
'bear': ['hibernates', 'North America', 'Brown', 'Fur', 'Strong'],
'owl': ['hoot', 'nocturnal', 'flies', 'big eyes', 'eats mice'],
'frog': ['pond', 'green', 'tongue', 'amphibian', 'eats flies'],
'toucan': ['rainbow', 'long beak', 'South America', 'tropical', 'wings'],
'monkey': ['eats bananas', 'trees', 'tail', 'swing', 'primate'],
'shark': ['ocean', 'dangeous', 'cartilege', 'sharp teeth', 'fins'],
'zebra': ['stripes', 'black and white', 'africa', 'safari', 'hoofs'],
'wolverine': ['vicious', 'skunk bear', 'brown', 'small', 'fast']}

randomanimal = random.choice(animal.keys())

values = animal.get(randomanimal)
print ("Let's play a game!")
print ("Guess the animal I'm thinking of.")

while True:
    guess = raw_input('What animal am I thinking of?')
    if guess == randomanimal:
        print "Correct guess. The animal i though of is %s" %randomanimal
        break
    else:
        print "nope"

答案 1 :(得分:0)

我想这是您正在寻找的代码。测试了这一点。完美地工作(Y)。试试这个。

import random
animal = {'lion': ['mane', 'teeth', 'pride', 'Africa', 'predator'],
'tiger': ['stripes', 'fur', 'endangered', 'cat', 'claws'],
'bear': ['hibernates', 'North America', 'Brown', 'Fur', 'Strong'],
'owl': ['hoot', 'nocturnal', 'flies', 'big eyes', 'eats mice'],
'frog': ['pond', 'green', 'tongue', 'amphibian', 'eats flies'],
'toucan': ['rainbow', 'long beak', 'South America', 'tropical', 'wings'],
'monkey': ['eats bananas', 'trees', 'tail', 'swing', 'primate'],
'shark': ['ocean', 'dangeous', 'cartilege', 'sharp teeth', 'fins'],
'zebra': ['stripes', 'black and white', 'africa', 'safari', 'hoofs'],
'wolverine': ['vicious', 'skunk bear', 'brown', 'small', 'fast']}

randomanimal = random.choice(dict(enumerate(animal)))
guess = ""
attributes = animal[randomanimal]
print ("Let's play a game!")
print ("Guess the animal I'm thinking of.")

while guess != randomanimal:
    if guess in attributes:
        print ("yes")
    elif guess in animal.keys():
        print ("Try Again")
    elif guess != "":
        print ("no")

    guess = raw_input('What animal am I thinking of?')

print ("You Win")

希望这有帮助。