1、学习使人秃头
2、学习使人秃头
3、学习使人秃头
4、以下是一些栗子,其中还有无数个bug
#在python2中这个object一定要写,python3中可写可不写(默认继承了object类)
class Student(object):
"""
第二步初始化,初始化自己
当你的类有一些公用变量的时候,你就可以初始化
初始化中经常放入公有的变量
"""
def __init__(self,name):
"""
在类中一切的变量和函数都要印记(self)
"""
self.name=name
def def1(self,num):#self不是参数,是印记,属于Student类
self.num=num
print(self.num)
print(self.name)
def def2(self):
print(self.num)
print(self.name)
if __name__=="__main__":
#实例,类名带括号“Student()”,直接运行初始化
student=Student(‘liyuan‘)
student.def1(100)
student.def2()
"""
王者荣耀
人机对战
1、输入人机对战
2、(典韦、赵云、鲁班)
3、显示战力,技能,防御
4、人物确定,随机出1个人机,包括战力、名字等信息显示
5、点击开始按钮
6、点击加载页面.....
"""
#这是我写的烂程序,还不能用
class Game(object):
def __init__(self):
#输入人机对战
def Step_1(self):
self.renji=input(‘Enter renji:‘)
Step_2()
#(典韦、赵云、鲁班),显示战力,技能,防御
def Step_2(self):
self.hero1=‘典韦‘
self.hero2=‘赵云‘
self.hero3=‘鲁班‘
self.choice=input(‘Enter your choice(典韦、赵云、鲁班):‘)
if self.choice in hero1:
self.print(‘战力:80% 技能:80% 防御:80%‘)
Step_3()
elif self.choice in hero2:
self.print(‘战力:70% 技能:60% 防御:50%‘)
Step_3()
else :
self.print(‘战力:50% 技能:10% 防御:1%‘)
Step_3()
#人物确定,随机出1个人机,包括战力、名字等信息显示
def Step_3(self):
import random
suiji=random.choice([‘典韦‘,‘赵云‘,‘鲁班‘])
if suiji == self.hero1:
print(‘战力:80% 技能:80% 防御:80%‘)
Step_4()
elif suiji == self.hero2:
print(‘战力:70% 技能:60% 防御:50%‘)
Step_4()
elif suiji == self.hero3:
print(‘战力:50% 技能:10% 防御:1%‘)
Step_4()
if __name__=="__main__":
Game()
#这是一个完整的王者小程序
import numpy as np
class wangzhe:
def __init__(self):
"""
初始化王者农药类.
Arguments:1
----------
zhaoyun[zhaoyun_xx]:关于人物赵云的参数
dianwei[dianwei_xx]:关于人物典韦的参数
luban[luban_xx]:关于人物鲁班的参数
"""
self.zhaoyun = ‘[1] 赵云‘
self.zhaoyun_zhanli = 250
self.zhaoyun_fangyu = 250
self.dianwei = ‘[2] 典韦‘
self.dianwei_zhanli = 300
self.dianwei_fangyu = 1000
self.luban = ‘[3] 鲁班‘
self.luban_zhanli = 10000
self.luban_fangyu = 10
def choose_model(self):
"""
选择模式
-------
可选模式暂时只有一个1.
"""
mode = input(‘可选模式[1/2]:1.人机对战,2.多人对战‘)
if mode == ‘1‘:
self.choose_people()
else:
print(‘?? 正在开发中...不要着急!‘)
def choose_people(self):
"""
用户选择人物..
"""
people = input(‘请选择你的英雄:(%s,%s,%s)‘%(self.zhaoyun,self.dianwei,self.luban))
if people == ‘1‘:
print(‘?? 赵云:‘)
self.information(self.zhaoyun_zhanli,self.zhaoyun_fangyu)
self.random_people()
elif people == ‘2‘:
print(‘?? 典韦:‘)
self.information(self.dianwei_zhanli,self.dianwei_fangyu)
self.random_people()
elif people == ‘3‘:
print(‘?? 鲁班:‘)
self.information(self.luban_zhanli,self.luban_fangyu)
self.random_people()
else:
print(‘(⊙_⊙)? 不懂你的选择‘)
self.choose_people()
def information(self,zhanli,fangyu):
"""
打印各个人物的信息
Arguments:
---------
zhanli: 人物的战力参数
fangyu: 人物的防御参数
"""
print(‘战力:%s,防御%s‘%(zhanli,fangyu))
def random_people(self):
"""
电脑随机选择人物..
"""
print(‘?? 电脑开始选择英雄:‘)
people = np.random.choice([‘1‘,‘2‘,‘3‘])
if people == ‘1‘:
print(‘?? 和你对战的是赵云:‘)
self.information(self.zhaoyun_zhanli,self.zhaoyun_fangyu)
elif people == ‘2‘:
print(‘?? 和你对战的是典韦:‘)
self.information(self.dianwei_zhanli,self.dianwei_fangyu)
elif people == ‘3‘:
print(‘?? 和你对战的是鲁班:‘)
self.information(self.luban_zhanli,self.luban_fangyu)
self.start()
def start(self):
"""
运行选择的模式
"""
print(‘[+] 马上进入游戏...‘)
def Runing(self):
"""
启动函数...
"""
print(‘[+] 正在加载...‘)
self.choose_model()
if __name__ == "__main__":
runing = wangzhe()
runing.Runing()
#变量名前面加两个__就是私有变量
#如果非要使用私有变量 那么可以使用dir(class())去查看他真正的名字
#私有变量或函数在类内部可以直接调用
#为了体现函数的重要性可以使用单下划线
import requests
import re
response=requests.get(‘http://www.89ip.cn‘)
HTML=response.text
# print(HTML)
compile_=re.compile(r‘(((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3})‘)
res=compile_.findall(HTML)
for ip_ in res:
print(ip_[0])