Dolcerena2015/2/3Python练习笔记1.简单循环#diceimportrandomforxinrange(1,11):throw_1=random.randint(1,6)throw_2=random.randint(1,6)total=throw_1+throw_2print(total)iftotal==7:print('SevenThrown!')iftotal==11:print('ElevenThrown!')ifthrow_1==throw_2:print('Doublethrown!')iftotal=5andtotal=9:print('notbad!')#:让这一行都变成注释import:调用库函数for…in…:构成一个循环,for后面跟一个变量名,每次循环后被赋一个新值(类似于C里面的i);in后面会计算出一套循环并列出来;后面的冒号说明还没有写完,接着要进入forin的内部(下一行自动缩进)and:两边为真则为真(相当于C里面的&&)iftotal=5andtotal=9:这句话还可以这样写ifnot(total5andtotal9):另外,C里面的||(或)在Python里面是or【注意】print括号里面引用要打印的字符用单引号,不是双引号;严格按照缩进(缩进在python里相当于C里面的{})。#dice_elifimportrandomforxinrange(1,11):throw_1=random.randint(1,6)throw_2=random.randint(1,6)total=throw_1+throw_2print(total)iftotal4:print('Badluck!')eliftotal8:print('Notgood')else:print('Notbad!')Dolcerena2015/2/3elif:elseif的缩写注意else后面的冒号2.#DRY#Don’trepeatyourself拒绝重复!#dice_while_breakimportrandomwhileTrue:throw_1=random.randint(1,6)throw_2=random.randint(1,6)total=throw_1+throw_2print(total)ifthrow_1==6andthrow_2==6:breakprint('DoubleSixthrown!')3.字符串book_name=‘ProgrammingRaspberryPi’若直接输入book_name若输入print(book_name)#区别:第一个输出一个字符串(带引号),第二个打印一个值(不带引号)字符串长度:len(book_name)获取字符串中指定位置字符:book_name[1]截取字符串:book_name[0:11]把字符串加到一起:book_name+‘byDolcerena’【注意】数组下标参数用方括号;首字母的位置从0开始;输入下标超出字符串长度会报错;截取时输入第二个数字“11”,其实取到字符串的第10个字符;如果不确定取到哪里,可以[12:](或者[:12])这样会默认取到最后(或者开头)。4.列表字符串是字符的列表。numbers=[123,34,55,321,9]给列表numbers赋初值(可以用len()得numbers的长度:5)numbers[1:3]取numbers里的2,3项numbers[0]=1将numbers里的第一项用“1”覆盖numbers.sort()对numbers里的值进行排序numbers.pop()移除列表中的一项,若括号中没有声明移除哪一项,则默认移除最后一项numbers.insert(1,66)在列表中增加一项,1代表插入位置,66代表插入内容Dolcerena2015/2/3big_list=[123,'hello',['innerlist',2,True]]复合列表,结构如下图:big_list→123“hello”→‘innerlist’2True思考1:如何取出2?big_list[2][1](将”big_list[2]”看做一个列表,列表后加[1]取出当前列表第二项,即2)思考2:设计一个for循环,将列表中的项列出来list=[1,'one',111]foriteminlist:print(item)【注意】pop()括号中的参数也是从0开始,即pop(1)移除列表中第二项;其他也是。5.函数功能:创造一个函数make_polite,让句子变得礼貌。#functiondefmake_polite(sentence):polite_sentence=sentence+'please'returnpolite_sentenceprint(make_polite('passthebook'))def:关键词,后跟函数名,最后加冒号return:函数返回值,不是每个函数必须有的小游戏:吊死鬼第一步:从几个单词中挑选一个作为被猜单词#hangman_pick_a_wordimportrandomwords=['period','record','field','object','physical']defpick_a_word():word_position=random.randint(0,len(words)-1)returnwords[word_position]print(pick_a_word())【这句话用来测试是否能够随机选出单词,测完需删掉】livesremaining=14【全局变量剩余生命值】Dolcerena2015/2/3guessed_letters=‘’第二步:写出play函数,再去写play中用到但还没有定义的函数defplay():word=pick_a_word()whileTrue:guess=get_guess(word)ifprocess_guess(guess,word):print('YouWin!WellDone!')breakiflives_remaining==0:print('YouareHung!')print('Thewordwas:'+word)break第三步:完善get_gusss函数,实现读取玩家输入字符defget_guess(word):print_word_with_blanks(word)print('LivesRemaining:'+str(lives_remaining))guess=input('Guessaletterorwholeword?')returnguessdefprint_word_with_blanks(word):print('print_word_with_blanks(word):notdoneyet')【“还没有完成”——这只是个桩函数,因为还没来得及写】第四步:完善process_guess函数与print_word函数defprint_word_with_blanks(word):display_word=''#空的(没有猜中任何一个字母)#forletterinword:#把输入的字母与word里的单词逐个比较#ifguessed_letters.find(letter)-1:#letterfound#内建函数find,不同返回-1相同返回字母位置#display_word=display_word+letterelse:#letternotfounddisplay_word=display_word+'_'print(display_word)defprocess_guess(guess,word):globallives_remainingglobalguessed_letterslives_remaining=lives_remaining-1guessed_letters=guessed_letters+guess#把猜过的字母放入guessed_letters#Dolcerena2015/2/3returnFalse第五步:两个选择,输入一个字母或整个单词把process_guess函数做大的改动defprocess_guess(guess,word):iflen(guess)1:returnwhole_word_guess(guess,word)else:returnsingle_letter_guess(guess,word)定义单个字母函数defsingle_letter_guess(guess,word):globallives_remainingglobalguessed_lettersifword.find(guess)==-1:#wordguesswasincorrectlives_remaining=lives_remaining-1guessed_letters=guessed_letters+guessifall_letters_guessed(word):#每次调用一遍,检查是否已经全部猜中#returnTruereturnFalsedefall_letters_guessed(word):forletterinword:ifguessed_letters.find(letter)==-1:returnFalsereturnTrue与整个单词函数defwhole_word_guess(guess,word):globallives_remainingifguess.lower()==word.lower():#把字母都转化成小写后比较#returnTrueelse:lives_remaining=lives_remaining-1returnFalse整个游戏:#hangmanimportrandomlives_remaining=14Dolcerena2015/2/3guessed_letters=''words=['period','record','field','object','physical']defpick_a_word():word_position=random.randint(0,len(words)-1)returnwords[word_position]defplay():word=pick_a_word()whileTrue:guess=get_guess(word)ifprocess_guess(guess,word):print('YouWin!WellDone!')breakiflives_remaining==0:print('YouareHung!')print('Thewordwas:'+word)breakdefget_guess(word):print_word_with_blanks(word)print('LivesRemaining:'+str(lives_remaining))guess=input('Guessaletterorwholeword?')returnguessdefprint_word_with_blanks(word):display_word=''forletterinword:ifguessed_letters.find(letter)-1:#letterfounddisplay_word=display_word+letterelse:#letternotfounddisplay_word=display_word+'_'print(display_word)defprocess_guess(guess,word):iflen(guess)1:returnwhole_word_guess(guess,word)Dolcerena2015/2/3else:returnsingle