制作一个Python动物猜谜游戏

-2 投票
2 回答
5813 浏览
提问于 2025-05-01 05:36

我正在上一个基础编程课,我们需要做一个动物猜谜游戏。这个游戏的基本想法是创建一个动物和它们特征的字典,然后让用户猜特征,程序会输出“是”或“不是”。如果他们猜对了动物的名字,程序就会显示“你赢了”或者“再试一次”。我在让代码正确运行时遇到了困难,特别是当用户输入动物的名字或特征时,程序没有输出正确的回应。任何帮助都会非常感谢。

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

我想这就是你要找的代码。我试过了,运行得非常好(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")

希望这对你有帮助。

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"

撰写回答