
python 字符串
文章平均质量分 50
妙舞汉宫人
这个作者很懒,什么都没留下…
展开
-
Python 用文件保存游戏(1)
from random import randint#读取文件中的成绩数据f=open('game.txt')score=f.read().split()#分别存入变量中game_times=int(score[0])min_times=int(score[1])total_times=int(score[2])#计算游戏的平均轮数,注意浮点数和避免除零错误if game_转载 2016-07-22 15:28:08 · 647 阅读 · 0 评论 -
Python 对文件内容迭代 按字节处理
def process(string): print 'Processing: ',stringf=open(r'D:\\ruanjian\\Python\\程序\\1.txt')while True: char=f.read(1) if not char:break process(char)f.close()Processing: tPr转载 2016-12-19 21:39:05 · 2583 阅读 · 0 评论 -
Python 随机数
import randoma=random.randint(1,100)print ('a='+str(a))b=random.randint(3, 3)print ('b='+str(b))c=random.random() #生成一个0到1之间的随机浮点数,包括0但不包括1,也就是[0.0, 1.0)print ('c='+str(c))d=random.uniform(2转载 2016-08-08 09:46:53 · 570 阅读 · 0 评论 -
Python 写文件
f=file('data.txt') #从data中读内容,保存至data1中data=f.read()print dataout=open('data1.txt','w')out.write(data)out.close()结果:I love you!hahahabiubiubiu!!!(在文件夹中创建了名为data1的txt文件,里面的内转载 2016-07-20 13:08:40 · 277 阅读 · 0 评论 -
Python 面向对象(2)-2
class MyClass: #类 name='Sam' #类变量 def sayHi(self): #类方法的第一个参数必须是self print 'Hello %s'%self.namemc=MyClass() #对象print mc.name #调用类变量的方法是“对象.变量名”mc.name='Lily转载 2016-07-29 09:39:05 · 224 阅读 · 0 评论 -
Python 面向对象(4)
class Vehicle: def __init__(self, speed): self.speed = speed def drive(self, distance): print 'need %f hour(s)' % (distance / self.speed)class Bike(Vehicle):转载 2016-07-29 15:22:06 · 285 阅读 · 0 评论 -
Python 面向对象(3)
#面向过程speed=60.0distance=100.0time =distance/speedprint timeprint '--------'#面向对象class Car: speed=0 def drive(self,distance): time =distance/self.speed #必须用self.speed,否则用的是第一次的s原创 2016-07-29 09:54:55 · 302 阅读 · 0 评论 -
Python 面向对象(2)-1
class MyClass: pass #空的代码块mc=MyClass()print mc结果: #mc是_main_模块中MyClass类的一个实例(Instance),后面的遗传16进制的数字是这个对象的内存地址转载 2016-07-29 09:37:38 · 186 阅读 · 0 评论 -
Python 面向对象
s='how are you'#s字符串类型的对象I=s.split()#split是字符串的方法,返回一个list类型的对象 #I是list类型的对象#dir(s)#dir(list)显示:>>>dir(s)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq_原创 2016-07-29 09:24:09 · 196 阅读 · 0 评论 -
python 字符串
print 'goood'str='bad'print strprint "It's good"print 'You are a "BAD" man'print 'I\'m a \"good\" teacher'print "this is the\same line"print '''"What's your name?"I asked."I'm Han Meimei."'原创 2016-07-13 17:14:17 · 274 阅读 · 0 评论 -
Python 字符串格式化
print 'My age is' + str(18)str1='good'str2='bye'print str1+str2num= 18print 'My age is' + str( num )num= 20print 'My age is %d'%numprint 'Price is %f'%4.99print 'Price is %.2f'%4.99name='cro转载 2016-07-13 17:37:05 · 310 阅读 · 0 评论 -
Python 字符串格式化 数字游戏
from random import randintnum=randint(1,67)print 'Guess what I think?'bingo =Falsewhile bingo==False: answer = input() if answer<num: print '%d is too small!'%answer if answer>原创 2016-07-13 17:57:26 · 276 阅读 · 0 评论 -
python 点球小游戏
from random import choicescore_you=0score_com=0#默认初始分均为0direction =['left','center','right']for i in range(5): print '===Round %d - You Kick!==='%(i+1) print 'Choice one side to转载 2016-07-17 14:50:25 · 914 阅读 · 0 评论 -
Python 点球小游戏
from random import choicescore=[0,0]direction =['left','center','right']def kick(): print '===You Kick!===' print 'Choice one side to kick:' print 'left ','center ','rig转载 2016-07-17 15:20:09 · 473 阅读 · 0 评论 -
Python 用文件保存游戏(2)
from random import randint#读取文件中的成绩数据f=open('game.txt')#注意文件名要正确score=f.read().split()f.close()#分别存入变量中game_times=int(score[0])min_times=int(score[1])total_times=int(score[2])#计算游戏的平均轮数,转载 2016-07-22 15:25:25 · 1219 阅读 · 0 评论 -
Python 对文件内容迭代 按行处理
def process(string): print 'Processing: ',stringf=open(r'D:\\ruanjian\\Python\\程序\\1.txt')while True: line=f.readline() if not line:break process(line)f.close()Processing: th转载 2016-12-19 21:41:14 · 3076 阅读 · 0 评论