Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python to write scissors stone paper game

Shulou Source: shulou.com Published: 2022-06-01 13:07:37 09月20日 Update

This article mainly explains "how to write scissors stone and paper games with Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to write a game of rock and paper scissors with Python".

1. The situation of computer winning

Computer (computer) player (player) rock (stone) scissors (scissors) scissors (scissor) paper (cloth) paper (cloth) stone (stone)

2. The situation in which players win

Player (player) computer (computer) stone (stone) scissors (scissors) scissors (scissor) paper (cloth) paper (cloth) stone (stone)

According to the above rules, we can do this in the form of if. The code is as follows:

Import random

Centering G = ['stone','scissor','cloth']

Computer = random.choice (clockG)

Player = input ('please enter your choice (stone,scissor,cloth):')

If computer = 'stone':

If player = 'stone':

Print ('\ 033 [33mdraw\ 033 [0m')

Elif player = 'scissor':

Print ('\ 033 [32myou loseboxes!\ 033 [0m')

Else:

Print ('\ 033 [31myou windowed trees!\ 033 [0m')

If computer = 'scissor':

If player = 'scissor':

Print ('\ 033 [33mdraw\ 033 [0m')

Elif player = 'stone':

Print ('\ 033 [31myou windowed trees!\ 033 [0m')

Else:

Print ('\ 033 [32myou loseboxes!\ 033 [0m')

If computer = 'cloth':

If player = 'cloth':

Print ('\ 033 [33mdraw\ 033 [0m')

Elif player = 'scissor':

Print ('\ 033 [31myou windowed trees!\ 033 [0m')

Else:

Print ('\ 033 [32myou loseboxes!\ 033 [0m')

Print ('your choice:%s' player,'computer choice:%s' computer)

Advanced level one

You can write down the winning situation in a list so as to make the above script more concise.

Import random

Centering G = ['stone','scissor','cloth']

Computer = random.choice (clockG)

WinList = [['stone','scissor'], [' scissor','cloth'], ['cloth','stone']]

Player = input ('please enter your choice (stone,scissor,cloth):')

If computer = = player:

Print ('\ 033 [33mdraw\ 033 [0m')

Elif [player,computer] in WinList:

Print ('\ 033 [33myou windowed!\ 033 [0m')

Else:

Print ('\ 033 [32myou loseboxes!\ 033 [0m')

Print ('your choice:%s' player,'computer choice:%s' computer)

Advanced level two

In order to make it more convenient to play, we use the numbers 0,1,2 instead of: stone (stone), scissor (paper), paper (cloth).

Import random

Centering G = ['stone','scissor','cloth']

Computer = random.choice (clockG)

WinList = [['stone','scissor'], [' scissor','cloth'], ['cloth','stone']]

Choice_memu ='

(0) stone

(1) scissor

(2) cloth

Please choice (0ram 1amp 2):'

Number = int (input (choice_memu))

Player = CG [number]

If computer = = player:

Print ('\ 033 [33mdraw\ 033 [0m')

Elif [player,computer] in WinList:

Print ('\ 033 [33myou windowed!\ 033 [0m')

Else:

Print ('\ 033 [32myou loseboxes!\ 033 [0m')

Print ('your choice:%s' player,'computer choice:%s' computer)

Advanced level three

We use the means of two wins in three games to determine the final champion. If it is a draw, continue to guess until the computer has won two games or the player has won two games.

Import random

Centering G = ['stone','scissor','cloth']

Computer = random.choice (clockG)

WinList = [['stone','scissor'], [' scissor','cloth'], ['cloth','stone']]

Choice_memu ='

(0) stone

(1) scissor

(2) cloth

Please choice (0ram 1amp 2):'

C_win = 0

P_win = 0

D_win = 1

While c_win < 2 and p_win < 2:

Number = int (input (choice_memu))

Player = CG [number]

If computer = = player:

D_win+=1

Elif [player,computer] in WinList:

P_win+=1

If p_win = = 2:

Print ('\ 033 [31myou windowed trees!\ 033 [0m')

Break

Else:

C_win+=1

If c_win = = 2:

Print ('\ 033 [32myou loseboxes!\ 033 [0m')

Break

Total_time = p_win + c_win + d_win

Print ('you guesse:% s times'% total_time,'you lost:% s times'% clockwyn win:% s times'% paired winCool draw:% s times'% d_win)

At this point, I believe you have a deeper understanding of "how to use Python to write scissors stone and paper game". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Scissors stone player computer situation advanced content champion learning practical deeper second code interest practicality practical draw form means easy to operate. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Microsoft macOS Redmi Shulou Technology