您的位置:首页 > 编程语言 > Python开发

Python(编程小白的第一本 Python 入门书) 学习笔记2——代码练习

2017-05-27 17:06 1331 查看
来源:编程小白的第一本 Python 入门书:http://www.ituring.com.cn/book/1863

学习上面链接内容,自己敲了一遍里面的练习。。。。

前奏、先来个helloworld

print('HelloWorld')


好了,下面正式开始………..

一、初级难度:设计一个重量转换器,输入以“g”为单位的数字后返回换算成“kg”的结果。

# 一、初级难度:设计一个重量转换器,输入以“g”为单位的数字后返回换算成“kg”的结果。
def g2kg(g):
return str(g/1000)+'kg'
print(g2kg(2000))


二、中级难度:设计一个求直角三角形斜边长的函数(两条直角边为参数,求最长边) 如果直角边边长分分别为3和4,那么返回的结果应该像这样:

def Pythagorean_theorem(a,b):
# 等价于a的平方与b的平方之和的1/2次方(即开跟)
return 'The right triangle third side\'s length is {}'.format((a**2 + b**2)**(1/2))
print(Pythagorean_theorem(3,4))


三、传递参数的方式有两种:位置参数(positional argument) 关键词参数(keyword argument)

求梯形面积。

#三传递参数的方式有两种:位置参数(positional argument)  关键词参数(keyword argument)
# 求梯形面积。
def trapezoid_area(base_up,base_down,height):
return 1/2*(base_down+base_up)*height
# (位置参数)
print(trapezoid_area(1,2,3))
# (关键词参数。)
print(trapezoid_area(base_up=1, base_down=2, height=3))


四、画树

# 四、画树
print('  *',' * *','* * *','  |  ',sep='\n');


五、设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名。

# 练习题
# 五、设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名。(这题不会)
def text_creation():
path='/Users/zzp/Desktop/w/'
for name in range(1,11):
with open(path+str(name)+'.txt','w') as text:
text.write(str(name))
text.close()
print('Done')
text_creation()


六、复利是一件神奇的事情,正如富兰克林所说:“复利是能够将所有铅块变成金块的石头”。设计一个复利计算函数 invest(),它包含三个参数:amount(资金),rate(利率),time(投资时间)。 输入每个参数后调用函数,应该返回每一年的资金总额。它看起来就应该像这样(假设利率为5%)

# 六、复利是一件神奇的事情,正如富兰克林所说:“复利是能够将所有铅块变成金块的石头”。设计一个复利计算函数 invest(),
# 它包含三个参数:amount(资金),rate(利率),time(投资时间)。
# 输入每个参数后调用函数,应该返回每一年的资金总额。它看起来就应该像这样(假设利率为5%)
def count(amount,rate,time):
i=0
while i<time:
i+=1
# 自己少写了个*(乘号)
amount=amount*(1+rate)
print('year {}:${}'.format(i,amount))
count(1000,0.01,8)


七、打印1~100内的偶数

# 七、打印1~100内的偶数     注意:range包含起点不包含终点
def even_print():
for i in range(1,101):
if i%2==0:
print(i)
even_print()


八、综合练习

# 1
list=[1,2,3]
print(sum(list))


#2 随机数  导入一个 random 的内置库
import random
p1=random.randrange(1,7)
p2=random.randrange(1,7)
p3=random.randrange(1,7)
print(p1,p2,p3)


正题来了:游戏开始,首先玩家选择 Big or Small(押大小),选择完成后开始摇三个骰子计算总值,

11 <= 总值 <=18 为 “大”,3 <= 总值 <= 10 为 “小”。然后告诉玩家猜对或是猜错的结果。

# total
# 游戏开始,首先玩家选择 Big or Small(押大小),选择完成后开始摇三个骰子计算总值,
# 11 <= 总值 <=18 为 “大”,3 <= 总值 <= 10 为 “小”。然后告诉玩家猜对或是猜错的结果。
import random
def roll_dice(numbers=3,points=None):
print('<<<<< ROLL THE DICE! >>>>>')
if points is None:
points=[]
while numbers>0:
point=random.randrange(1,7)
points.append(point)
numbers=numbers-1
return points

def roll_result(total):
isBig=11<=total<=18
isSmall=3<=total<=10
if isBig:
return 'Big'
elif isSmall:
return  'Small'

def start_game():
print('<<<<<
cf65
GAME STARTS! >>>>>')
choices=['Big','Small']
your_choice=input('Big or Ssmall:')
if your_choice in choices:
points=roll_dice()
total=sum(points)
youWin=your_choice==roll_result(total)
if youWin:
print('The points are',points,'You win !')
else:
print('The points are', points, 'You lose !')
else:
print('Invalid Words')
start_game()

start_game()


九、在最后一个项目的基础上增加这样的功能,下注金额和赔率。具体规则如下:初始金额为1000元;金额为0时游戏结束;默认赔率为1倍,也就是说押对了能得相应金额,押错了会输掉相应金额。

# 练习题
# 九、在最后一个项目的基础上增加这样的功能,下注金额和赔率。具体规则如下:
# 初始金额为1000元;
# 金额为0时游戏结束;
# 默认赔率为1倍,也就是说押对了能得相应金额,押错了会输掉相应金额。
import random

# 设置moneyAll为全局变量,注意:哪里需要全局变量,哪里声明一下;
global moneyAll
moneyAll = 1000

def roll_dice(numbers=3,points=None):
print('<<<<< ROLL THE DICE! >>>>>')
if points is None:
points=[]
while numbers>0:
point=random.randrange(1,7)
points.append(point)
numbers=numbers-1
return points

def roll_result(total):
isBig=11<=total<=18
isSmall=3<=total<=10
if isBig:
return 'Big'
elif isSmall:
return  'Small'

def start_game():
# 这个需要使用到上面设置的全局变量,所以要在这重新声明
global moneyAll
print('<<<<< GAME STARTS! >>>>>')
choices=['Big','Small']
your_choice=input('Big or Ssmall:')
if your_choice in choices:

# 注意,在这里犯了一个错,input输入的是字符串,要使用数字运算、比较需要自己强转为int**********
your_bet = int(input('How much you wanna bet?-'))
if moneyAll>=your_bet:
points=roll_dice()
total=sum(points)
youWin=your_choice==roll_result(total)
if youWin:
print('The points are',points,'You win !')
moneyAll+=your_bet
print('You gained',your_bet,'You have',moneyAll,'now')
start_game()
else:
print('The points are', points, 'You lose !')
moneyAll -= your_bet
print('You lost', your_bet, 'You have', moneyAll, 'now')
if moneyAll==0:
print('GAME OVER')
else:
start_game()
else:
print('not full money')
start_game()
else:
print('Invalid Words')
start_game()

start_game()


十、我们在注册应用的时候,常常使用手机号作为账户名,在短信验证之前一般都会检验号码的真实性,如果是不存在的号码就不会发送验证码。检验规则如下:

长度不少于11位;

是移动、联通、电信号段中的一个电话号码;

因为是输入号码界面,输入除号码外其他字符的可能性可以忽略;

移动号段,联通号段,电信号段如下:

CN_mobile = [134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705]

CN_union = [130,131,132,155,156,185,186,145,176,1709]

CN_telecom = [133,153,180,181,189,177,1700]

# 十、我们在注册应用的时候,常常使用手机号作为账户名,在短信验证之前一般都会检验号码的真实性,如果是不存在的号码就不会发送验证码。检验规则如下:
# 长度不少于11位;
#   是移动、联通、电信号段中的一个电话号码;
#   因为是输入号码界面,输入除号码外其他字符的可能性可以忽略;
#   移动号段,联通号段,电信号段如下:
#       CN_mobile = [134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705]
#       CN_union = [130,131,132,155,156,185,186,145,176,1709]
#       CN_telecom = [133,153,180,181,189,177,1700]

def chephone():
phoneNumber=input('Enter Your number:')
numlenth=len(phoneNumber)
CN_mobile = [134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 147, 178, 1705]
CN_union = [130, 131, 132, 155, 156, 185, 186, 145, 176, 1709]
CN_telecom = [133, 153, 180, 181, 189, 177, 1700]
first_three=int(phoneNumber[0:3])
first_four=int(phoneNumber[0:4])
if numlenth!=11:
print('Invalid length,your number should be in 11 digits')
chephone()
elif first_three in CN_mobile or first_four in CN_mobile:
print('Opetator:China Mobile')
print('We\re sending verification code via ttext to your phone:',phoneNumber)
elif first_three in CN_union or first_four in CN_union:
print('Opetator:China Union')
print('We\re sending verification code via ttext to your phone:', phoneNumber)
elif first_three in CN_telecom or first_four in CN_telecom:
print('Opetator:China Telecom')
print('We\re sending verification code via ttext to your phone:', phoneNumber)

else:
print('No such a operator')
chephone()

chephone()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python