for loop - MasterMind Game Python Code -



for loop - MasterMind Game Python Code -

import random def create_code(characters,length): """(str,int) -> list homecoming list of length size of single character of characters in given str >>>create_code('ygbopr') ['g','b','r','b'] """ characters = 'grbyop' length = 4 homecoming list(random.sample(characters,length)) pass def find_fully_correct(answer, guess): """(list,list) -> list homecoming list containing 'b' each correctly positioned color in guess >>>find_fully_correct(['g','b','r','b'], ['g','b','r','b']) ['b', 'b', 'b', 'b'] """ res= [] x, y in zip(guess, answer): if x == y: res.append("b") homecoming res if res else none pass def remove_fully_correct(answer, guess): """(list,list) -> list homecoming list removes chars first list same , in same postion in sec list >>>remove_fully_correct(['a','b','c','d'], ['d','b','a','d']) ['a','c'] """ res= reply x,y in zip(answer, guess): if x == y: res.remove(x) list3 = remove_fully_correct(guess,answer) list4 = remove_fully_correct(answer,guess) char in list4: if char in list3: homecoming res pass def find_colour_correct(answer, guess): """(list,list) -> list homecoming list of 'w''s number of 'w''s qual number of strs in sec list have same value str in first list different position >>>find_colour_correct(['y','n','g','g'], ['g','n','o','r']) ['w'] """ res = [] str in guess: if str in answer: res.append("w") homecoming res pass def print_game(guesses, clues): """(list,list) -> display print display headers, guess , clue, corresponding sublists of given lists >>>print_game(guesses,clues) guesses clues o o o o b r r r r b b """ print ('guesses \t clues') guess = '' clue = '' guesses = guess_list clues = clue_list clue_list = (find_fully_correct, find_correct,colour) guess_list in guesses: index in range(len(guess_list)): guess = guess + ' ' + guess_list[index] guess = guess.lstrip() clue_list in clues: char in range (len(clue_list)): clue = clue + ' ' + clue_list[char] clue = clue.lstrip() print (guess + ' \t ' + clue + '\n') pass def valid(user_guess, valid_chars, size): if len(user_guess) != size or user_guess not in valid_chars: homecoming false else: homecoming true pass if __name__ == '__main__': size = 4 tries = 10 valid_chars= 'grbyop'

attempting code mastermind game involves creation of hidden colour combination or code - consisting of 4 colour code chosen 6 colours program. user code-breaker , tries figure out code guessing code. programme gives feedback on correctness of guess telling sec player how many correctly positioned in guessed colour code has , how many right colour incorrectly positioned colours guess has, until user either guesses actual colour code or runs out of 10 tries.

i'm having problem print_game function , valid function

also i'm not sure how command actual programme function game after if__name__ == 'main' section. in calling actual functions giving commands user see progress.

any help much appreciated.

if want inquire user input, can utilize raw_input in python 2.x, , input in python 3. start in order test code.

valid seems conceptually right me except pass statement @ end unnecessary. can rewritten as:

valid(user_guess, valid_chars, size): homecoming len(user_guess) == size , reduce(operator.and , map(lambda x: x in valid_chars ,user_guess))

i not sure trying accomplish print_game. given list of guesses , clues print_game should be:

print_game(guesses,clues): print ('guesses \t clues') assert len(guesses) == len(clues) g,c in zip(guesses,clues): print g,'\t',c

python for-loop boolean

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -