python學習 演算法 石頭剪刀布

2021-09-12 07:05:34 字數 1456 閱讀 4750

1.玩家 控制台輸入:石頭(1)/剪刀(2)/布(3)

2.電腦隨機輸入石頭/剪刀/布

3.判斷勝負:石頭win剪刀,剪刀win布,布win石頭

import random

player =

int(

input

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

computer = random.randint(1,

3)print

("player出了{},computer出了{}"

.format

(player,computer)

)if player ==1:

if computer ==1:

print

("平局"

)elif computer ==2:

print

("player勝"

)else

:print

("computer勝"

)elif player ==2:

if computer ==1:

print

("computer勝"

)elif computer ==2:

print

("平局"

)else

:print

("player勝"

)else

:if computer ==1:

print

("player勝"

)elif computer ==2:

print

("computer勝"

)else

:print

("平局"

)

import random

player =

int(

input

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

computer = random.randint(1,

3)print

("玩家出了{},電腦出了{}"

.format

(player,computer))if

((player ==

1and computer ==2)

or(player ==

2and computer ==3)

or(player ==

3and computer ==1)

):print

("player勝"

)# 只要相同,就是平局。平局如果在else下面實現,則此處**量會大大增加。

elif player == computer:

print

("平局"

)else

:print

("computer勝"

)

不知道兩種方案那個效率高,在實際中怎麼用?

Python 剪刀石頭布

python 剪刀石頭布 by 鄭瑞國 import random mydict while true 提示獲取玩家的輸入 player int input 請輸入 0剪刀 1石頭 2布 讓電腦隨機出乙個 computer random.randint 0,2 print player mydict...

Python 剪刀石頭布

author cch coding utf 8 time 2020 9 30 10 53 file demo3.py software pycharm 剪刀 0,石頭 1,布 2 使用者輸入數字0 2中的乙個數字,與系統隨機生成的數字比較後,給出結果 import random x random.r...

剪刀石頭布

開始學習時寫的剪刀石頭布 雖然是用c 寫的 我感覺思想還是面向過程 慚愧慚愧 include include include include includeusing namespace std enum caiquan class inte cet inte cet inte cet system...