这篇“如何用Python做一个骰子模拟器和石头剪刀布游戏”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“如何用Python做一个骰子模拟器和石头剪刀布游戏”文章吧。
一、骰子模拟器
目标:做出一个可以掷骰子游戏的程序。
tips:在用户询问时,用 random 模块去生成 1 到 6 间的数字。
二、石头剪刀布游戏
目标:做出一个命令行游戏,玩家可以在石头、剪刀和布之间选择,和计算机进行对决。如果玩家获胜,得分会添加,游戏结束后最终分数展示给玩家。
tops:收到玩家选择,和计算机选择比较。计算机的选择是在列表随机选中。如果玩家获胜会增加一分。
import random
choices = ["Rock", "Paper", "Scissors"]
computer = random.choice(choices)
player = False
cpu_score = 0
player_score = 0
while True:
player = input("Rock, Paper or Scissors?").capitalize()
# 判断游戏者和电脑的选择
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"CPU:{cpu_score}")
print(f"Plaer:{player_score}")
break
else:
print("That's not a valid play. Check your spelling!")
computer = random.choice(choices)