【问题标题】:Rand Dict value not working in pythonRand Dict 值在 python 中不起作用
【发布时间】:2016-06-15 11:34:42
【问题描述】:

我有什么方法可以指定我想要的“ben Stiller”或“bill murray”的值吗?

我知道我不能做 random.choice(ben_stiller.values()):

我还更改了字典,所以关键是电影的名称,现在可以完美运行。

^

import random

last_man = {
    'Mark Walberg': '1',
    'Ben Stiller': '2',
    'Bill Murray': '3',

}

markymark = {
    'movie': 'The martian',
    'movie': 'The Departed',
    'movie': 'XYZ'
}

ben_stiller = {
    'movie': 'Meet the parents',
    'movie': 'Something about mary',
    'movie': 'dunno'
}

bill_murray = {
   'movie': 'Life Aquatic',
   'movie': 'rushmore',
   'movie': 'Groundhogs day'
}

print "There are three actors, one will be chosen randomly"
print "You have to name as many movies as they have been in"
print "If you get one wrong, you lose, you hae three chances, and must     get one correct"

random.choice(last_man.keys())

print "The actor randonly chosen is: ", random.choice(last_man.keys())

guesses_taken = 0

while guesses_taken <= 3:
    guess = raw_input(': ')
    guesses_taken = guesses_taken + 1

    if guess == random.choice(bill_murray.keys()):
        print "you won in %s guesses" % guesses_taken
        print guesses_taken
    elif guess == random.choice(ben_stiller.keys()):
        print "you won in %s guesses" % guesses_taken
        print guesses_taken
    elif guess == random.choice(markymark.keys()):
        print "you won in %s guesses" % guesses_taken
        print guesses_taken
    elif guess != random.choice(markymark.keys()):
        print "You lost"
        print guesses_taken
    else:
        print "you lose"
        exit(0)

【问题讨论】:

  • 您可能需要重新考虑一下字典的工作原理。字典每个键只能有一个值。在字典文字的情况下,最后一个获胜,例如bill_murray 仅包含 {'movie': "Groundhogs day"}。在您的情况下,keys() 始终返回 ['movie']
  • 您在if 语句末尾缺少:
  • ... 和 : 在所有 elif 语句的末尾...。了解版本控制,以便在破坏代码时发现差异。
  • 回复:'现在可以忽略这个'。如果我是你,我会删除这个问题。特别是当您编辑代码以纠正问题时:)
  • 我实际上在工作版本中,有电影名称的键和电影的值。有什么办法可以做 random.choice(bill_murray.values()): ?我知道这不起作用,但我怎样才能检索到值?我想我可以使用密钥来生成它的值。

标签: python dictionary random


【解决方案1】:

完整的错误系统

File "movie.py", line 42
  if guess == random.choice(bill_murray.keys())
                                             ^
SyntaxError: invalid syntax

所以,语法无效。 if 行需要以 : 结尾

如果我们通过添加冒号来整理第 42 行,那么我们得到

File "movie.py", line 45
  elif guess == random.choice(ben_stiller.keys())
                                              ^
SyntaxError: invalid syntax

所以,语法无效。 elif 行需要以 : 结尾

elif guess == random.choice(ben_stiller.keys()):
#                                              ^---- and so on 

语法错误现在消失了。 现在我们还有另一个问题。 还有一个错别字

“你有三个机会,”

->

“你有三个机会,”

无论如何。如果它向我展示了

随机选择的演员是:比尔·默里

(好吧,两个错别字……)

我选择“rushmore”,它告诉我我错了。正如 cmets 所指出的,当我尝试向字典中的现有键(“电影”)添加值时,它会被替换。我们能不能给个电影清单?

另外,当我播放这个时,它没有使用电影:

The actor randonly chosen is:  Bill Murray
: rushmore
You lost
1
: Groundhogs day
You lost
2
: Life Aquatic
You lost
3
: movie
you won in 4 guesses
4

让我们改用电影列表。作为演员姓名的键的值。 并删除exit(0)

类似这样的:

import random

last_man = {
    'Mark Walberg': ['The martian', 'The Departed', 'XYZ'],
    'Ben Stiller': ['Meet the parents', 'Something about mary', 'dunno'],
    'Bill Murray': ['Life Aquatic', 'rushmore', 'Groundhogs day']

}


print "There are three actors, one will be chosen randomly"
print "You have to name as many movies as they have been in"
print "If you get one wrong, you lose, you have three chances, and must get one correct"

actor = random.choice(last_man.keys())
#^--- remember who

print "The actor randomly chosen is: ", actor

guesses_taken = 0

while guesses_taken <= 3:
    guess = raw_input(': ')
    guesses_taken = guesses_taken + 1

    if guess in last_man[actor]:
        print "you won in %s guesses" % guesses_taken
        print guesses_taken
        break
    else:
        print "you lose"

【讨论】:

  • 非常感谢,我已经接近使用你对演员和嵌套电影列表所做的 dict 格式了。我刚刚将该格式用于 diff dict 设置,类似但带有州、它们的缩写和人口。至于拼写错误——我知道他们在那里,只是看看代码是否有效。最初,我正确设置了它,它会给我一个随机演员,并有三次机会猜测他的三部电影中的一部,而且效果很好。我不知道是什么导致它出错了,确实发生了一些事情。我希望我能调出那个版本,我仍然可以在我的终端中看到它正常工作。
  • 我知道这是一种奇怪的使用 dict 的方式,但是因为我是全新的,所以我学习的任何东西我都倾向于最大限度地使用并以不寻常的方式使用,看看我是否可以弄清楚如何实现它。在这种情况下,我确实弄明白了,但后来改变了它以更新打印,不知何故把它搞砸了。这是学习python的愚蠢方式吗?
  • 尝试一下,看看会发生什么总是好的。但请务必了解版本控制(例如 git),以便在出现问题时轻松跟踪更改的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 2015-03-09
  • 2016-06-18
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
相关资源
最近更新 更多