Python做一个骰子模拟器和石头剪刀布游戏

2021-04-06 11:12:23 浏览数 (34)

我们通过 Python 的第三方库可以完成很多操作,下面为大家介绍如何用 Python 制作一个骰子模拟器和石头剪刀布游戏。

一、骰子模拟器

目标:做出一个可以掷骰子游戏的程序。

tips:在用户询问时,用 random 模块去生成 1 到 6 间的数字。

python掷骰子

二、石头剪刀布游戏

目标:做出一个命令行游戏,玩家可以在石头、剪刀和布之间选择,和计算机进行对决。如果玩家获胜,得分会添加,游戏结束后最终分数展示给玩家。

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)

以上就是小编为您带来的关于 Python做一个骰子模拟器和石头剪刀布游戏 的全部内容。


1 人点赞