程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

剪刀石頭布小游戲,python實現

編輯:Python

人生中的第一篇博客,剪刀石頭布的小游戲

# -*- codeing = utf-8 -*-
# @Time : 2021/10/22 14:36
# @Author : 寧洪康
# @File : 剪刀石頭布.py
# @Software : PyCharm
import random
choices = ["Rock", "Scissors", "Paper"]
computer = random.choice(choices)
player = False
cpu_score = 0 #電腦初始成績
player_score = 0 #玩家初始成績
while True:
player = input("Rock, Scissors or Paper" ).title()
# 判斷電腦與玩家選擇
if player == computer:
print("tie!")
elif player == "Rock":
if computer == "Paper":
print("you lose!", computer, "covers", player)
cpu_score += 1
else:
print("you win!", player, "smashes", computer)
player_score += 1
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
cpu_score += 1
else:
print("You win!", player, "covers", computer)
player_score += 1
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
cpu_score += 1
else:
print("You win!", player, "cut", computer)
player_score += 1
elif player == 'E': #輸出成績,跳出循環
print("Final Scores:")
print(f"computer:{cpu_score}")
print(f"Player:{player_score}")
break
else:
print("That's not a valid play. Check your spelling!")
computer = random.choice(choices)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved