猜拳游戏的实现
import random # 導(dǎo)入產(chǎn)生隨機(jī)數(shù)字的模塊# 開(kāi)發(fā)步驟
# 1. 接收用戶(hù)出的拳,石頭(1), 剪刀(2), 布(3)player = int(input("請(qǐng)出拳 石頭(1), 剪刀(2), 布(3):"))
# 2. 讓電腦隨機(jī)出拳
computer = random.randint(1, 3) # [1, 3]
print("player:", player, type(player))
print("computer:", computer, type(computer))
# 3. 比較勝負(fù)
# 3.1 你贏了
# 3.1.1 當(dāng)用戶(hù)出石頭想要贏,電腦必須出剪刀
# 3.1.2 當(dāng)用戶(hù)出剪刀想要贏,電腦必須出布
# 3.1.3 當(dāng)用戶(hù)出布想要贏,電腦必須出石頭
# 以上三種贏的情況是或者的關(guān)系
if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):print("您輸了")
# 3.2 平局 用戶(hù)出的拳和電腦出拳一樣
elif player == computer:print("平局")
# 3.3 您輸了
else:print("您贏了")#打印結(jié)果:
#請(qǐng)出拳 石頭(1), 剪刀(2), 布(3):2
#player: 2 <class 'int'>
#computer: 3 <class 'int'>
#您輸了進(jìn)程已結(jié)束,退出代碼0
import random
# ==============根據(jù)以下注釋,完成相關(guān)的代碼練習(xí)==========# 根據(jù)猜拳游戲的需求,寫(xiě)出猜拳游戲的功能代碼
# 開(kāi)發(fā)步驟
# 1. 接收用戶(hù)出的拳,石頭(1), 剪刀(2), 布(3)# 2. 讓電腦隨機(jī)出拳# 3. 比較勝負(fù)
# 3.1 你贏了
# 3.1.1 當(dāng)用戶(hù)出石頭想要贏,電腦必須出剪刀
# 3.1.2 當(dāng)用戶(hù)出剪刀想要贏,電腦必須出布
# 3.1.3 當(dāng)用戶(hù)出布想要贏,電腦必須出石頭
# 以上三種贏的情況是或者的關(guān)系# 3.2 平局 用戶(hù)出的拳和電腦出拳一樣# 3.3 您輸了printer=int(input('請(qǐng)輸入用戶(hù)數(shù)據(jù):'))computer=random.randint(1,3)
print("printer",printer,type(printer))
print("computer",computer,type(printer))
if (computer==1 and printer==3)or(computer==2 and printer==1) or (computer==3 and printer==2):print("我贏了")elif(computer==1 and printer==1)or(computer==2 and printer==2) or (computer==3 and printer==3):print('平局')
else:print('我輸了')#打印結(jié)果:
#請(qǐng)輸入用戶(hù)數(shù)據(jù):2
#printer 2 <class 'int'>
#computer 3 <class 'int'>
#我贏了進(jìn)程已結(jié)束,退出代碼0
?
總結(jié)
- 上一篇: outlook没有显示已连接到服务器上,
- 下一篇: 控制用计算机论文,计算机在自动控制技术实