Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python to make a dice simulator and a paper game of rock scissors

Shulou Source: shulou.com Published: 2022-06-01 13:18:16 10月01日 Update

This article "how to use Python to do a dice simulator and rock scissors paper game" the knowledge points of most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "how to use Python to do a dice simulator and rock scissors paper game" article.

I. Dice Simulator

Goal: make a program that can play dice games.

Tips: when asked by the user, use the random module to generate numbers between 1 and 6.

Second, rock scissors paper game

Goal: to make a command-line game in which players can choose between stone, scissors and paper and face off with the computer. If the player wins, the score will be added and the final score will be displayed to the player at the end of the game.

Tops: receive player selections and compare with computer selections. The selection of the computer is randomly selected in the list. If the player wins, it will be increased by one point.

Import randomchoices = ["Rock", "Paper", "Scissors"] computer = random.choice (choices) player = Falsecpu_score = 0while True: player = input ("Rock, Paper or Scissors?"). Capitalize () # judge the choice between the player and the computer 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) above is about "how to use Python to do a dice simulator and rock scissors paper game" the content of this article, I believe we all have a certain understanding, I hope the content shared by the editor will be helpful to you, if you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Tags: Stone dice content player selection simulator knife cloth computer article goal knowledge article between value score scissors command most that is score Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker OPPO Reno Linux Xiaomi vpn