掌握知识点: I/O、 数据 、字体
1.Python数据类型
2.获取用户输入
3.处理异常
4.Mad Lib 游戏
5.操作二进制文件
6.Trivia游戏
==============
Python数据类型
+=============
str() int() float() 用于数据类型的转换
Python的数据类型比较灵活的定义,和C语言进行对比,十分友好。
擅长处理成组的信息,例如 mylist = [1,2,3,4,5,6]
===========
用户输入
+==========
一般使用 raw_input() 函数 和 input() 函数
在Python 2.7中有所不同
区别如下:
a = input()
print a * 2
输入: 123
结果: 246
a = raw_input()
print a * 2
输入:123
结果:123123
一个是数字,一个是字符串。
============
异常处理
+===========
使用 try…except语句块完成异常处理,会捕捉错误。
问题代码加在try:语句块中,有错误时会运行except : 中的代码,
number = input("Enter a number: ")
try:
number = float(s)
except:
number = 0
print "It's not a number."
answer = number * number
print( number, "*", number, "=", answer )
=============
Mad Lib 游戏 (文字游戏)
+============
函数: string.replace() 进行搜索–替换 操作
print 'MAD LIB GAME !'
guy = raw_input('what\'s your name? ')
arm = raw_input('what arm do you have ?')
place_to_go = raw_input('where to go ?')
thing_to_do = raw_input('what do you want to do ?')
monster = raw_input('Monster is ?')
start_time = raw_input('when start to go ?')
end_time = raw_input('when end ?')
result = 'you are winner'
story = """
My name is GUY, I want to go to PLACE. Because I want to beat a Monster, I have a ARM so I decide to THING ,
I will go at START , and back at END.
Finally, The GUY fight whith MONSTER, result is RESULT.
"""
dic = {
'GUY' : guy, 'ARM':arm, 'PLACE': place_to_go, 'THING': thing_to_do, 'START': start_time, 'END': end_time, 'RESULT': result, 'MONSTER': monster}
for i in dic :
story = story.replace(i, dic[i])
print story
=========
文件输入和输出
+========
使用open()函数
#向文件写入内容
file = open('1.txt', 'w')
file.write("Hello python!\n")
file.write("Hello , wor