Python基礎學習筆記 random模組

2021-09-25 12:53:22 字數 1029 閱讀 4024

# 匯入隨機數庫

import random

# 1. 接收使用者輸入的拳

player =

int(

input

("請出拳 石頭(1) 剪刀(2) 布(3): "))

# 2. 讓電腦隨機出拳,隨機數(1-3)

computer = random.randint(1,

3)# 隨機生成是1到3之間的數字,[1,3]

print

(computer)

# 3. 比較勝負

first =

(player ==

1and computer ==2)

second =

(player ==

2and computer ==3)

three =

(player ==

3and computer ==1)

if first or second or three:

print

("您贏了"

)elif player == computer:

print

("平局"

)else

:print

("您輸了,電腦贏了"

)

函式

含義random()生成乙個[0, 1.0)之間的隨機浮點數

uniform(a, b)生成乙個ab之間的隨機浮點數

randint(a, b)生成乙個ab之間的隨機整數,包含ab

choice()從列表中隨機返回乙個元素

shuffle()將列表中元素隨機打亂

sampl(, k)從指定列表中隨機獲取k個元素

python學習筆記 Python基礎

雲計算web開發 django框架 科學計算 人工智慧 常用到的庫有numpy pandas matplotlib 等等 系統運維 金融 圖形gui google 豆瓣 知乎 facebook 主要特點是 解釋性 動態語言 強型別定義語言和弱型別定義語言。解釋型 python擁有良好的相容性,在安裝...

Python學習筆記(Python基礎)

1 資料型別和變數 1 字串是以單引號 或雙引號 括起來的任意文字,比如 abc xyz 等 2 如果 本身也是乙個字元,那就可以用 括起來,比如 i m ok 包含的字元是i,m,空格,o,k這6個字元。3 如果字串內部既包含 又包含 可以用轉義字元 來標識 print i m ok 結果為i m...

python隨想之python中的range

python3中range 的用法 在python3中range 函式返回的是乙個可迭代物件 不是列表型別 需要自己轉換成列表 python3 list 函式是物件迭代器,可以把range 返回的可迭代物件轉為乙個列表,返回的變數型別為列表。函式的語法 range stop range start,...