Rand Dict值在python中不起作用

时间:2016-06-15 11:34:42

标签: python dictionary random

我有什么方法可以指定' ben stiller'或者'比尔默里我想要的?

我知道我不能做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)

1 个答案:

答案 0 :(得分:0)

完整的错误消息系统

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 

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

  

&#34;你有三次机会,&#34;

- &GT;

  

&#34;你有三次机会,&#34;

反正。如果它给我提供

  

randonly选择的演员是:Bill Murray

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

我选择&#34; rushmore&#34;它告诉我,我错了。正如评论所指出的,当我尝试在字典中为现有密钥(&#34; movie&#34;)添加值时,它会被替换。我们可以只列出一部电影吗?

另外,当我播放时,它不会使用电影:

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"