一、設計這樣一個函數,在指定的文件夾上創建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( )