mercredi 15 juin 2016

Rand Dict value not working in python

IS there any way for me to specify the VALUE of 'ben stiller' or 'bill murray that I want?

I know that I cant do random.choice(ben_stiller.values()):

I also changed the dict so the key is the name of the movie and works perfect now.

^

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)




Aucun commentaire:

Enregistrer un commentaire