Coding With Fun
Home Docker Django Node.js Articles FAQ

Python does a dice simulator and stone scissors cloth game


May 29, 2021 Article blog



We can do a lot with Python's third-party library, and here's how to make a dice simulator and stone scissors with Python.

First, the dice simulator

Goal: Make a program that can roll the dice.

Tips: Use the random module to generate 1 to 6 numbers when asked by the user.

 Python does a dice simulator and stone scissors cloth game1

Second, stone scissors cloth game

Goal: Make a command-line game in which players can choose between stones, scissors, and cloth to play against a computer. If the player wins, the score is added and the final score is displayed to the player after the game is over.

tops: Receives player selection, compared to computer selection. T he computer's selection is randomly selected in the list. If the player wins, one point will be added.

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)

That's all the little editor brings you about Python doing a dice simulator and stone scissors cloth game.