Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple stone scissors paper game by python

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report