mooc课程mit6.00.1x--problemset3解决方法

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

RADIATION EXPOSURE

挺简单的一道题,计算函数和算法过程都已经给出,做一个迭代计算就行了。1defradiationExposure(start, stop, step):2'''3Computes and returns the amount of radiation exposed4to between the start and stop times. Calls the5function f (defined for you in the grading script)6to obtain the value of the function at any point.78start: integer, the time at which exposure begins9stop: integer, the time at which exposure ends10step: float, the width of each rectangle. You can assume that11the step size will always partition the space evenly.1213returns: float, the amount of radiation exposed to14between start and stop times.15'''16#FILL IN YOUR CODE HERE...17area =018whilestart <stop:19area = area + f(start) *step20start +=step2122returnarea

AWordGAME: HANGMAN

一个猜单词游戏,个人感觉略有难度,游戏规则见这里。将其分步实现相应的函数。1#6.00PRoblem Set 32#3#Hangman game4#56#-----------------------------------7#Helper code8#You don't need to understand this helper code,9#but you will have to know how to use the functions10#(so be sure to read the docstrings!)1112importrandom13importstring1415WORDLIST_FILENAME ="words.txt"1617defloadWords():18"""19Returns a list of valid words. Words are strings of lowercase letters.2021Depending on the size of the word list, this function may22take a while to finish.23"""24print"Loading word list from file..."25#inFile: file26inFile = open(WORDLIST_FILENAME,'r', 0)27#line: string28line =inFile.readline()29#wordlist: list of strings30wordlist =string.split(line)31print"", len(wordlist),"words loaded."32returnwordlist3334defchooseWord(wordlist):35"""36wordlist (list): list of words (strings)37Returns a word from wordlist at random38"""39returnrandom.choice(wordlist)4041#end of helper code42#-----------------------------------4344#Load the list of words into the variable wordlist45#so that it can beaccessed from anywhere in the program46wordlist =loadWords()4748defisWordGuessed(secretWord, lettersGuessed):49'''50secretWord: string, the word the user is guessing51lettersGuessed: list, what letters have been guessed so far52returns: boolean, True if all the letters of secretWord are in53lettersGuessed; False otherwise54'''55#FILL IN YOUR CODE HERE...56forcinsecretWord:57ifcnotinlettersGuessed:58returnFalse5960returnTrue6162defgetGuessedWord(secretWord, lettersGuessed):63'''64secretWord: string, the word the user is guessing65lettersGuessed: list, what letters have been guessed so far66returns: string, comprised of letters and underscores that represents67what letters in secretWord have been guessed so far.68'''69#FILL IN YOUR CODE HERE...70guess =""71forcinsecretWord:72ifcinlettersGuessed:73guess +=c74else:75guess +="_"7677returnguess7879defgetAvailableLetters(lettersGuessed):80'''81lettersGuessed: list, what letters have been guessed so far82returns: string, comprised of letters that represents what letters have not83yet been guessed.84'''85#FILL IN YOUR CODE HERE...86available =""87forcinstring.ascii_lowercase:88ifcnotinlettersGuessed:89available +=c9091returnavailable9293defhangman(secretWord):94'''95secretWord: string, the secret word to guess.96Starts up an interactive game of Hangman.97* At the start of the game, let the user know how many98letters the secretWord contains.99* Ask the user to supply one guess (i.e. letter) per round.100* The user should receive feedback immediately after each guess101about whether their guess appears in the computers word.102* After each round, you should also display to the user the103partially guessed word so far, as well as letters that the104user has not yet guessed.105Follows the other limitations detailed in the problem write-up.106'''107#FILL IN YOUR CODE HERE...108print"Welcome to the game Hangman!"109print"I am thinking of a word that is", len(secretWord),"letters long."110print"-------------"111lettersGuessed =[]112mistakesMade =0113availableLetters =string.ascii_lowercase114115whilemistakesMade < 8andnotisWordGuessed(secretWord, lettersGuessed):116print"You have", 8 - mistakesMade,"guesses left."117print"Available letters:", availableLetters118guess = raw_input("Please guess a letter:").lower()119120ifguessinlettersGuessed:121print("Oops! You've already guessed that letter:"+122getGuessedWord(secretWord, lettersGuessed))123elifguessinsecretWord:124lettersGuessed.append(guess)125print"Good guess:", getGuessedWord(secretWord, lettersGuessed)126else:127mistakesMade += 1128lettersGuessed.append(guess)129print("Oops! That letter is not in my word:"+130getGuessedWord(secretWord, lettersGuessed))131132availableLetters =getAvailableLetters(lettersGuessed)133134print"------------"135136ifisWordGuessed(secretWord, lettersGuessed):137print"Congratulations, you won!"138else:139print("Sorry, you ran out of guesses. The word was"+ secretWord +140".")141142#When you've completed your hangman function, uncomment these two lines143#and run this file to test! (hint: you might want to pick your own144#secretWord while you're testing)145146secretWord =chooseWord(wordlist).lower()147hangman(secretWord)

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航