Python编程入门练习笔记.docx_第1页
Python编程入门练习笔记.docx_第2页
Python编程入门练习笔记.docx_第3页
Python编程入门练习笔记.docx_第4页
Python编程入门练习笔记.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Dolcerena 2015/2/3Python练习笔记1. 简单循环#diceimport randomfor x in range(1,11): throw_1 = random.randint(1,6) throw_2 = random.randint(1,6) total = throw_1 + throw_2 print(total) if total = 7: print(Seven Thrown!) if total = 11: print(Eleven Thrown!) if throw_1 = throw_2: print(Double thrown!) if total = 5 and total = 5 and total = 9:这句话还可以这样写if not (total 9):另外,C里面的|(或)在Python里面是or【注意】print括号里面引用要打印的字符用单引号,不是双引号;严格按照缩进(缩进在python里相当于C里面的)。#dice_elifimport randomfor x in range(1,11): throw_1 = random.randint(1,6) throw_2 = random.randint(1,6) total = throw_1 + throw_2 print(total)if total 4: print(Bad luck!) elif total book_name = Programming Raspberry Pi若直接输入book_name若输入print(book_name)#区别:第一个输出一个字符串(带引号),第二个打印一个值(不带引号)字符串长度:len(book_name)获取字符串中指定位置字符:book_name1截取字符串:book_name0:11把字符串加到一起:book_name + by Dolcerena【注意】数组下标参数用方括号;首字母的位置从0开始;输入下标超出字符串长度会报错;截取时输入第二个数字“11”,其实取到字符串的第10个字符;如果不确定取到哪里,可以12:(或者:12)这样会默认取到最后(或者开头)。4.列表字符串是字符的列表。 numbers = 123,34,55,321,9给列表numbers赋初值(可以用len()得numbers的长度:5) numbers1:3取numbers里的2,3项 numbers0 = 1将numbers里的第一项用“1”覆盖 numbers.sort()对numbers里的值进行排序 numbers.pop()移除列表中的一项,若括号中没有声明移除哪一项,则默认移除最后一项 numbers.insert(1,66)在列表中增加一项,1代表插入位置,66代表插入内容 big_list = 123,hello,inner list,2,True复合列表,结构如下图:big_list123“hello”inner list2True思考1:如何取出2? big_list21(将”big_list2”看做一个列表,列表后加1取出当前列表第二项,即2)思考2:设计一个for循环,将列表中的项列出来list = 1,one,111for item in list: print(item)【注意】pop()括号中的参数也是从0开始,即pop(1)移除列表中第二项;其他也是。5.函数功能:创造一个函数make_polite,让句子变得礼貌。#functiondef make_polite(sentence): polite_sentence = sentence + please return polite_sentenceprint(make_polite(pass the book)def:关键词,后跟函数名,最后加冒号return:函数返回值,不是每个函数必须有的小游戏:吊死鬼第一步:从几个单词中挑选一个作为被猜单词#hangman_pick_a_wordimport randomwords = period, record,field,object,physicaldef pick_a_word(): word_position = random.randint(0,len(words)-1) return wordsword_positionprint(pick_a_word()【这句话用来测试是否能够随机选出单词,测完需删掉】lives remaining = 14【全局变量剩余生命值】guessed_letters = 第二步:写出play函数,再去写play中用到但还没有定义的函数def play(): word = pick_a_word() while True: guess = get_guess(word) if process_guess(guess,word): print(You Win! Well Done!) break if lives_remaining = 0: print(You are Hung!) print(The word was: + word) break第三步:完善get_gusss函数,实现读取玩家输入字符def get_guess(word): print_word_with_blanks(word) print(Lives Remaining: + str(lives_remaining) guess = input(Guess a letter or whole word?) return guessdef print_word_with_blanks(word): print(print_word_with_blanks(word):not done yet)【“还没有完成”这只是个桩函数,因为还没来得及写】第四步:完善process_guess函数与print_word函数def print_word_with_blanks(word): display_word = #空的(没有猜中任何一个字母)# for letter in word:#把输入的字母与word里的单词逐个比较# if guessed_letters.find(letter) -1: #letter found#内建函数find,不同返回-1相同返回字母位置# display_word = display_word +letter else: #letter not found display_word = display_word + _ print(display_word)def process_guess(guess,word): global lives_remaining global guessed_letters lives_remaining = lives_remaining - 1 guessed_letters = guessed_letters + guess#把猜过的字母放入guessed_letters# return False第五步:两个选择,输入一个字母或整个单词把process_guess函数做大的改动def process_guess(guess,word): if len(guess) 1: return whole_word_guess(guess,word) else: return single_letter_guess(guess,word)定义单个字母函数 def single_letter_guess(guess,word): global lives_remaining global guessed_letters if word.find(guess) = -1: #word guess was incorrect lives_remaining = lives_remaining - 1 guessed_letters = guessed_letters + guess if all_letters_guessed(word):#每次调用一遍,检查是否已经全部猜中# return Truereturn False def all_letters_guessed(word): for letter in word: if guessed_letters.find(letter) = -1: return False return True与整个单词函数def whole_word_guess(guess,word): global lives_remaining if guess.lower() = word.lower():#把字母都转化成小写后比较# return True else: lives_remaining = lives_remaining - 1 return False整个游戏:#hangmanimport randomlives_remaining = 14guessed_letters = words = period, record,field,object,physicaldef pick_a_word(): word_position = random.randint(0,len(words)-1) return wordsword_positiondef play(): word = pick_a_word() while True: guess = get_guess(word) if process_guess(guess,word): print(You Win! Well Done!) break if lives_remaining = 0: print(You are Hung!) print(The word was: + word) break def get_guess(word): print_word_with_blanks(word) print(Lives Remaining: + str(lives_remaining) guess = input(Guess a letter or whole word?) return guessdef print_word_with_blanks(word): display_word = for letter in word: if guessed_letters.find(letter) -1: #letter found display_word = display_word + letter else: #letter not found display_word = display_word + _ print(display_word)def process_guess(guess,word): if len(guess) 1: return whole_word_guess(guess,word) else: return single_letter_guess(guess,word) def single_letter_guess(guess,word): global lives_remaining global guessed_letters if word.find(guess) = -1: #word guess was incorrect lives_remaining = lives_remaining - 1 guessed_letters = guessed_letters + guess if all_letters_guessed(word): return True return False def all_letters_g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论