這節主要介紹 time,random模塊;不用死記。首先說一下什么是模塊。。。
import time 有些朋友,開始比迷惑的,但是后面聊到類(屬性,方法),包會好一點,我們這里沒這么快說到,慢慢來,但是為了方便你記憶。。。最及簡單的是.....
你知道 x.py是你寫的python腳本;如果你在同一個目錄下 定義了另外一個 time.py。你import time就直接調用了(這也是,我們需要注意的,定義x.py時候名字不要跟自帶的模塊名字,第3方模塊一樣的名字,除非你自己想重寫他的模塊。。。。牛逼)
import time ###導入time的模塊,time.py文件的 變量(屬性);函數(方法)都可以使用。。。。
import random ###導入隨機數模塊。
print(time.time()) ###打印現在的 時間戳。。。如何轉換?搜索一下吧。
time.sleep(3) ###睡3秒
print(random.randint(1,100)) ###是不是打印一個 1 到 100 的整數?
number = range(1,100) ###number 變成一個列表了。
print(random.sample(number,3)) ###是不是抽了3個數字?
####################我們模擬一個打架吧。。。
people_a_life = random.randint(150,200)
people_b_life = random.randint(150,200)
people_a_kill_power = random.randint(50,80)
people_b_kill_power = random.randint(50,80)
print("打手1\n 血量:%s 殺傷力:%s"%(people_a_life,people_a_kill_power))
time.sleep(2)
print("打手2\n 血量:%s 殺傷力:%s"%(people_b_life,people_b_kill_power))
while people_a_life >= 0 and people_b_life >=0:
people_b_life = people_b_life -people_a_kill_power
print("開始 打手1的***:%s, 打手2:%s"%(people_a_kill_power,people_b_life))
time.sleep(2)
people_a_life = people_a_life -people_b_kill_power
print("開始 打手1的***:%s, 打手2:%s"%(people_b_kill_power,people_a_life))
time.sleep(1)
if people_a_life > people_b_life:
print("打手1 win")
elif people_b_life > people_a_life:
print("打手2 win")
else:
print("打平")
####相信你開完,自己已經更加扎實了,本課,我們復習了
print()
\n ##轉義符號
%s ###占位符號
people_a_life ###變量
增加
import time 時間模塊 time.time();time.sleep(3)
import random 隨機模塊 random.randint(1,10) 取個隨機數。。。。。。。。。。
是不是很有動力,學習了。。。。打好基礎,什么都好搞,主要是什么?調試。。
調試過程中。。。。新手花時間,其實代碼人員也是調試花時間,然后是后續的修復bug......質量很重要。。。。一個合格的產品經理,很重要。。。。。。。。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
