Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize simple stone scissors paper game by python

Shulou Source: shulou.com Published: 2022-06-01 09:47:16 10月04日 Update

This article mainly introduces python how to achieve a simple stone scissors paper game, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Goal:

Use python judgment sentence to realize the game of rock scissors and paper.

Train of thought:

Suppose scissors (0), stone (1), paper (2), then how can we win?

So according to this table, you can write a preliminary code:

If user = = 0 and computer = = 0: print ("draw") elif user = = 0 and computer = = 1: print ("player wins") elif user = = 0 and computer = = 2: print ("computer win") elif user = = 1 and computer = = 0: print ("computer win") elif user = = 1 and computer = = 1: print ("draw") elif user = 1 and computer = 2: print ("player wins") elif user = 2 and computer = = 0: print ("player wins") elif user = 2 and computer = = 1: print ("computer wins") elif user = = 2 and computer = = 2: print ("draw")

When we finish writing this string of code, it is not difficult to find that it is too troublesome to write code like this, and everyone is afraid of trouble, so we can write shorter code according to the law.

According to the above table, we can easily find the pattern:

When 1.if user-computer =-2 or user-computer = = 1, when the player wins 2.if user-computer = =-1 or user-computer = = 2, when the computer wins 3.if user-computer = = 0, it is a draw

Then some of the simplified code is as follows:

If user = = computer: print ("player is% s, computer is% s, draw"% (usr,com)) elif user-computer =-1 or user-computer = = 2: print ("player is% s, computer is% s, player loses"% (usr,com)) else: print ("player is% s, computer is% s, player wins% (usr,com)

Because the computer is random and we don't know it, we need to call random. The complete code is as follows:

Import randomcomputer = random.randint (0jue 2) user = int (input ("scissors (0), rock (1), cloth (2):") # judge whether the computer produces rock, scissors, or paper if computer = = 0: com = "scissors" elif computer = = 1: com = "rock" else: com = "paper" # judge the player's rock, scissors Still cloth if user = 0: usr = "scissors" elif user = = 1: usr = "Rock" else: usr = "cloth" # result and output if user = = computer: print ("player is% s, computer is% s, draw% (usr,com)) elif user-computer = =-1 or user-computer = = 2: print (" player is% s, computer is% s, player loses% (usr,com)) else: print ("player is% s, computer is% s, computer is% s) Player wins "% (usr,com))

The illustration of the effect is as follows:

Thank you for reading this article carefully. I hope the article "how to achieve simple stone scissors paper game with python" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Players computer stone code scissors draw articles knives rules or trouble above table value interest at the same time ideas effects more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information Huawei Microsoft MariaDB