python-循环与判断练习题

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

def text_creation():
path ='D:/study/python3/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%):
def invest(amount,rate,time):
print("principal amount:{}".format(amount))
for t in range(1, time + 1):
amount = amount * (1+ rate)
print("year {}: ${}".format(t,amount))
invest(100, .05, 8)
invest(2000, .025, 5)

三、打印1~100内的偶数

第一步打印出1-100的数
for i in range(1,101):
print(i)
第二步打印出其中的偶数
def even_print():
for i in range(1,101):
if i % 2 == 0:
print(i )
even_print()

综合练习
1.求列表之和
a_list = [1,2,3]
print(sum(a_list))

2.输出随机数
import random

point1 = random.randrange(1,7)
point2 = random.randrange(1,7)
point3 = random.randrange(1,7)

print(point1,point2,point3)

导入一个 random 的内置库,每次打印结果肯定是不一样的,其中 random 中的 randrange 方法使用起来就像是 range 函数一样,两个参数即可限定随机数范围。

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

第2行:创建函数,设定两个默认参数作为可选,numbers --骰子数量,points一一三个骰子的点数的列表;
第3行:告知用户开始摇骰子;
第4-5行:如果参数中并未指定points,那么为points创建空的列表;
第6-9行:摇三次骰子,每摇一次numbers就减1,直至小于等于0时,循环停止;
第10行:返回结果的列表。

将点数化成大小
def roll_result(total):
isBig = 11 <= total <=18
isSmall = 3 <= total <=10
if isBig:
return 'Big'
elif isSmall:
return 'Small'
第1行:创建函数,其中必要的参数是骰子的总点数:
第2-3行:设定“大”与“小”的判断标准;
第4-7行:在不同的条件下返回不同的结果。

最后,创建一个开始游戏的函数,让用户输入猜大小,并且定义什么是猜对,什么是猜错,并输出对应的输赢结果。

最后,创建一个开始游戏的函数,让用户输入猜大小,并且定义什么是猜对,什么是猜错,并输出对应的输赢结果。
def start_game( ):
print('<<< GAME STARTS!>>>')
choices = ['Big','Small']
your_choice = input('Big or Small:')
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( )


第1行:创建函数,并不需要什么特殊参数;
第2行:告知用户游戏开始;
第3行:规定什么是正确的输入;
第4行:将用户输入的字符串储存在your_choice中
第5、13-15行:如果符合输入规范往下进行,不符合这告知用户并重新开始
第6行:调用roll_dice函数,将返回的列表命名为points;
第7行:点数求和;
第8行:设定胜利的条件——你所选的结果和计算机生成的结果是一致的;
第9-12行:成立则告知胜利,反之,告知失败;
第16行:调用函数,使程序运行。


增加个赌钱的判断
# -*- coding: utf-8 -*-

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( ):
your_money = 1000
while your_money > 0:
print('<<< GAME STARTS!>>>')
choices = ['Big','Small']
your_choice = input('Big or Small:')
if your_choice in choices:
your_bet = int(input('How much you wanna bet? -'))
points = roll_dice( )
total = sum(points)
youWin = your_choice == roll_result(total)
if youWin:
print('The points are',points,'You win !')
print('You gained {},you have {}now'.format(your_bet,your_money+your_bet))
your_money = your_money + your_bet
else:
print('The points are',points,'You lose!')
print('You gained {},you have {}now'.format(your_bet, your_money - your_bet))
your_money = your_money - your_bet
else:
print('Invalid Words')
else:
print('GAME OVER')
start_game( )













posted on 2016-10-03 15:55  键盘上的指尖  阅读(5819)  评论(0编辑  收藏  举报

导航