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