dictionary - Rand Dict value not working in python -
is there way me specify value of 'ben stiller' or 'bill murray want?
i know cant random.choice(ben_stiller.values()):
i changed dict key name of movie , 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 parents', 'movie': 'something mary', 'movie': 'dunno' } bill_murray = { 'movie': 'life aquatic', 'movie': 'rushmore', 'movie': 'groundhogs day' } print "there 3 actors, 1 chosen randomly" print "you have name many movies have been in" print "if 1 wrong, lose, hae 3 chances, , must 1 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)
the full error syas
file "movie.py", line 42 if guess == random.choice(bill_murray.keys()) ^ syntaxerror: invalid syntax
so, syntax invalid. if
line needs end :
if sort out line 42 adding colon, get
file "movie.py", line 45 elif guess == random.choice(ben_stiller.keys()) ^ syntaxerror: invalid syntax
so, syntax invalid. elif
line needs end :
elif guess == random.choice(ben_stiller.keys()): # ^---- , on
syntax errors gone. have problem. , typo
"you hae 3 chances, "
->
"you have 3 chances, "
anyway. if presents me with
the actor randonly chosen is: bill murray
(alright, 2 typos...)
and choose "rushmore" tells me wrong. comments have noted, when try add value existing key ("movie") in dictionary gets replaced. have list of movies?
also, when play isn't using movies:
the actor randonly chosen is: bill murray : rushmore lost 1 : groundhogs day lost 2 : life aquatic lost 3 : movie won in 4 guesses 4
let's use lists of movies instead. value key of actor's name. , remove exit(0)
something this:
import random last_man = { 'mark walberg': ['the martian', 'the departed', 'xyz'], 'ben stiller': ['meet parents', 'something mary', 'dunno'], 'bill murray': ['life aquatic', 'rushmore', 'groundhogs day'] } print "there 3 actors, 1 chosen randomly" print "you have name many movies have been in" print "if 1 wrong, lose, have 3 chances, , must 1 correct" actor = random.choice(last_man.keys()) #^--- remember 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"
Comments
Post a Comment