ProgrammingThinkingandMethod(6-7)ZhaoHai赵海puterScienceandEngineeringShanghaiJiaoTongUniversity ******@,1/,1/,1/,aprogramthatmaintainsamembershiplistforaclubmightneedtolookupinformationforaparticularmember–,1/eAsimpleSearchingProblemHereisthespecificationofasimplesearchingfunction:defsearch(x,nums):#numsisalistofnumbersandxisanumber#urs #or-:>>>search(4,[3,1,4,2,5])2>>>search(7,[3,1,4,2,5])-15PythonProgramming,1/eASimpleSearchingProblemInthefirstexample,,thereturnvalue--insearch-relatedmethods!6PythonProgramming,1/eASimpleSearchingProblemWecantesttoseeifavalueappearsinasequenceusingin.ifxinnums:#dosomethingIfwewanttoknowthepositionofxinalist,theindexmethodcanbeused.>>>nums=[3,1,4,2,5]>>>(4)27PythonProgramming,1/-,1/eASimpleSearchingProblemdefsearch(x,nums):try:(x)except:return-1Sure,thiswillwork,butwearereallyinterestedinthealgorithmusedtoactuallysearchthelistinPython!9Pytho
Programming Thinking and Method (6-7) 来自淘豆网www.taodocs.com转载请标明出处.