END:小练习、涨知识

python实用主义者
 
1.python给予解决问题最好的方案,其他语言C、java都是有多多种解决方案。
 
2.文件的写入
  f=open('D:/123.txt','w') #如果没有文件,那么会自动创建文件,但是文件夹系统不会自动创建
 f.write('党京伟,你好') #括号里面要写入要输入的内容
 f.close()
 
3.电话号码的替换
  phone_number='18792948424'
  hiding=phone_number.replace(phone_number[:6],'*'* 9) #括号里面是phone.replace[:10]并且新字符串是‘*’*9
  print(hiding)
 
4.查找电话号码中的数字
search = '168'
num_a = '1386-168-0006'
print(search+'是在第'+str(num_a.find(search))+'到第'+str((num_a.find(search)+len(search)))+'个之间')
 
5.填空,根据输入内容返回对应天气预报的城市
city = input("请输入需要查看天气的城市:")
url = "http://apistore.baidu.com/microservice/weather?citypinyin={}".format(city) #字符串填空共有3种方式:1.默认顺序的2.指定名称3.阿拉伯数字的,例子是第1种
print(url)
 
6.内置函数
    内置函数就是在安装完phthon后,系统自带的函数,共有68个,比如input(),print(),int(),dict(),str(),len(),format(),sum()等等。
 
7.练习
'''
输入g重量,返回kg重量
'''
def weight_change(weight):
    weight_convert=weight/1000
    print(str(weight_convert)+'kg')#如果用%d,则表示取整数部分
weight_change(900)
'''
勾股定理,求第三边的长度
'''
def triangle(a,b):
    return ('这个三角函数第3边长度是{}'.format((a**2+b**2)**(1/2)))
print(triangle(4,5))
 
8.创建简单过滤器:
'''
创建指定输入的文件名称并写入文件内容
'''
def text_create(name,context):
    file_path='D:/'
    full_path=file_path+name+'.txt'
    f=open(full_path,'w')
    f.write(context)
    f.close()
    print('DOWN')
text_create('djw','你好!!!')
'''
将敏感词过滤
'''
def word_filter(text,consorde_word='shit',change_word='good'):
    return text.replace(consorde_word,change_word)
print(word_filter('you are shit'))

'''
简单过滤器:将词语过滤后,写入对应的文件
'''
def consored_change_text(name,mags):
    word_new=word_filter(mags)
    text_create(name,word_new)
consored_change_text('djw2','you are shit and goods!')

9.运算符的使用
  print(334//10) #结果是33,取整
 
10.is 表示身份鉴定,in 表示归属
 
11.python的可嵌入性
  你可以把Python嵌入你的C/C++程序,从而向你的程序用户提供脚本功能
 
12.小游戏-掷塞子
'''
设计一个猜测大小的游戏
'''
import random
def roll_dice(number=3,list=None):
print('<<<<< ROLL THE DICE! >>>>>')
if list is None:
list=[]
while number>0:
num=random.randrange(1,7) #1到7之间的整数
list.append(num)
number-=1
return list
def roll_result(total):
if 11 <= total <= 18:
return 'big'
elif 3<=total<=10:
return 'small'
def start_game():
print('<<<<< GAME STARTS! >>>>>')
choice=['small','big']
your_choice=input('big or small')
if your_choice in choice:
list=roll_dice()
sums=sum(list)
result=roll_result(sums)
youwin=your_choice==result
if youwin:
print(sums,'you win!!!')
else:
print(sums, 'you lost!!!')
else:
print('Invalid Words')
start_game()
start_game()
 

猜你喜欢

转载自www.cnblogs.com/dangjingwei/p/12392113.html
end