In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.