剪刀石頭布
#導入隨機工具包
import random
player=int(input("請輸入剪刀(1)/剪刀(2)/布(3)"))
computer = random.randint(1, 3)
print("玩家選擇的是 %d - 電腦出的是%d" % (player,computer))
if((player == 1 and computer == 2)
or (player == 2 and computer == 3)
or(player == 3 and computer == 1)):
print("電腦弱爆,玩家勝利")
elif player == computer :
print("平局了")
else :
print("電腦勝利了")
知識點:
1、if 的條件語句比較多的時候,可以用換行8個空格優化一下條件語句的結構;注意和執行語句要有一行空格隔開。
2、import 可以導入隨機工具包,找到一個適合的工具random.randint(),來進行生成隨機數。
3、random.randint(a,b) 的用法, 生成一個X,a<= X <= b 且注意,a<=b